Browse Source

C加加懒人语音助手_微软晓晓帮你读字幕

蘭雅sRGB 1 week ago
parent
commit
84178603fd
2 changed files with 144 additions and 0 deletions
  1. 73 0
      speech_synthesis/MSYY.cpp
  2. 71 0
      speech_synthesis/my.py

+ 73 - 0
speech_synthesis/MSYY.cpp

@@ -0,0 +1,73 @@
+#include <windows.h>
+#include <process.h> // 添加头文件以使用 _spawnlp
+
+#define ID_BUTTON_TOOLS 1
+#define ID_BUTTON_MOVE 2
+#define ID_BUTTON_CLOSE 3
+
+LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
+  switch (uMsg) {
+  case WM_COMMAND:
+    switch (LOWORD(wParam)) {
+    case ID_BUTTON_TOOLS: {
+      // MessageBox(hwnd, "正在运行 Python 脚本...", "Info", MB_OK);
+      // 后台运行 python.exe my.py
+      _spawnlp(_P_NOWAIT, "python.exe", "python.exe", "my.py", NULL);
+    } break;
+
+    case ID_BUTTON_MOVE: {
+      MoveWindow(hwnd, GetSystemMetrics(SM_CXSCREEN) / 2, 0, 90, 30, TRUE);
+      break;
+    }
+    case ID_BUTTON_CLOSE:
+      PostMessage(hwnd, WM_CLOSE, 0, 0);
+      break;
+    }
+    break;
+  case WM_DESTROY:
+    PostQuitMessage(0);
+    return 0;
+  default:
+    return DefWindowProc(hwnd, uMsg, wParam, lParam);
+  }
+  return 0;
+}
+
+int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
+  const char CLASS_NAME[] = "NoTitleWindow";
+
+  WNDCLASS wc = {};
+  wc.lpfnWndProc = WindowProc;
+  wc.hInstance = hInstance;
+  wc.lpszClassName = CLASS_NAME;
+  wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
+  wc.style = CS_HREDRAW | CS_VREDRAW;
+
+  RegisterClass(&wc);
+  int windowWidth = 90;
+  int windowHeight = 30;
+  int x = (GetSystemMetrics(SM_CXSCREEN) - windowWidth) / 2;
+  int y = (GetSystemMetrics(SM_CYSCREEN) - windowHeight) / 2;
+
+  HWND hwnd = CreateWindowEx(WS_EX_TOPMOST, CLASS_NAME, NULL, WS_POPUP, x, y, windowWidth, windowHeight, NULL, NULL,
+                             hInstance, NULL);
+  if (hwnd == NULL) {
+    return 0;
+  }
+
+  HWND hButtonTools = CreateWindow("BUTTON", "MSYY", WS_VISIBLE | WS_CHILD | BS_FLAT, 0, 0, 60, 30, hwnd,
+                                   (HMENU)ID_BUTTON_TOOLS, (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), NULL);
+  HWND hButtonMove = CreateWindow("BUTTON", "V", WS_VISIBLE | WS_CHILD | BS_FLAT, 60, 15, 30, 15, hwnd,
+                                  (HMENU)ID_BUTTON_MOVE, (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), NULL);
+  HWND hButtonClose = CreateWindow("BUTTON", "X", WS_VISIBLE | WS_CHILD | BS_FLAT, 60, 0, 30, 15, hwnd,
+                                   (HMENU)ID_BUTTON_CLOSE, (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), NULL);
+  ShowWindow(hwnd, nShowCmd);
+  UpdateWindow(hwnd);
+
+  MSG msg;
+  while (GetMessage(&msg, NULL, 0, 0)) {
+    TranslateMessage(&msg);
+    DispatchMessage(&msg);
+  }
+  return 0;
+}

+ 71 - 0
speech_synthesis/my.py

@@ -0,0 +1,71 @@
+import os
+import azure.cognitiveservices.speech as speechsdk
+
+import win32clipboard as w
+
+def get_clipboard_text():
+    w.OpenClipboard()
+    try:
+        text = w.GetClipboardData()
+    except TypeError:
+        text = ""
+    w.CloseClipboard()
+    return text
+
+class SPEECH:
+    def __init__(self):
+        # 需要转语音的文本文字
+        # 设置音频保存本地地址
+        self.output_file = "r:\\output.wav"
+        # 从环境变量中获取订阅密钥和区域信息
+        self.speech_config = speechsdk.SpeechConfig(subscription=os.environ.get('SPEECH_KEY'),
+                                                    region=os.environ.get('SPEECH_REGION'))
+        # 设置语音合成使用的语言——  云枫  
+        self.speech_config.speech_synthesis_language = "zh-CN"
+        # self.speech_config.speech_synthesis_voice_name = 'zh-CN-YunfengNeural'    # 云枫
+        self.speech_config.speech_synthesis_voice_name = 'zh-CN-XiaoxiaoNeural'     # 晓晓
+
+    def speak_txt(self):
+        # 配置音频输出为默认扬声器
+        audio_config = speechsdk.audio.AudioOutputConfig(use_default_speaker=True)
+        return audio_config
+
+    def down_audio(self):
+        # 配置音频输出为默认扬声器
+        audio_config = speechsdk.audio.AudioOutputConfig(filename=self.output_file)
+        return audio_config
+
+    def run(self, text, audio_config):
+
+        # 创建语音合成器
+        speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=self.speech_config, audio_config=audio_config)
+
+        # 调用语音合成API将文本转换为语音
+        speech_synthesis_result = speech_synthesizer.speak_text_async(text).get()
+
+        # 检查语音合成的结果并进行相应的处理
+        if speech_synthesis_result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
+            print("Speech synthesized for text [{}]".format(text))
+        elif speech_synthesis_result.reason == speechsdk.ResultReason.Canceled:
+            cancellation_details = speech_synthesis_result.cancellation_details
+            print("Speech synthesis canceled: {}".format(cancellation_details.reason))
+            if cancellation_details.reason == speechsdk.CancellationReason.Error:
+                if cancellation_details.error_details:
+                    print("Error details: {}".format(cancellation_details.error_details))
+                    print("Did you set the speech resource key and region values?")
+
+
+if __name__ == "__main__":
+    my_speech = SPEECH()
+
+    # 从剪贴板获取要转换为语音的文本
+    text = get_clipboard_text()
+    
+    if not text:
+        print("剪贴板没有内容,请复制一些文本。")
+    else:
+        # 保存到本地
+        # my_speech.run(text, my_speech.down_audio())
+        print(text)
+        # 输出语音播放
+        my_speech.run(text, my_speech.speak_txt())