
- Python 基礎
- Python - 首頁
- Python - 概述
- Python - 歷史
- Python - 特性
- Python vs C++
- Python - Hello World 程式
- Python - 應用領域
- Python - 直譯器
- Python - 環境設定
- Python - 虛擬環境
- Python - 基本語法
- Python - 變數
- Python - 資料型別
- Python - 型別轉換
- Python - Unicode 系統
- Python - 字面量
- Python - 運算子
- Python - 算術運算子
- Python - 比較運算子
- Python - 賦值運算子
- Python - 邏輯運算子
- Python - 位運算子
- Python - 成員運算子
- Python - 身份運算子
- Python - 運算子優先順序
- Python - 註釋
- Python - 使用者輸入
- Python - 數字
- Python - 布林值
- Python 控制語句
- Python - 控制流
- Python - 決策
- Python - If 語句
- Python - If else
- Python - 巢狀 If
- Python - Match-Case 語句
- Python - 迴圈
- Python - for 迴圈
- Python - for-else 迴圈
- Python - While 迴圈
- Python - break 語句
- Python - continue 語句
- Python - pass 語句
- Python - 巢狀迴圈
- Python 函式 & 模組
- Python - 函式
- Python - 預設引數
- Python - 關鍵字引數
- Python - 僅關鍵字引數
- Python - 位置引數
- Python - 僅位置引數
- Python - 可變引數
- Python - 變數作用域
- Python - 函式註解
- Python - 模組
- Python - 內建函式
- Python 字串
- Python - 字串
- Python - 字串切片
- Python - 修改字串
- Python - 字串連線
- Python - 字串格式化
- Python - 跳脫字元
- Python - 字串方法
- Python - 字串練習
- Python 列表
- Python - 列表
- Python - 訪問列表項
- Python - 修改列表項
- Python - 新增列表項
- Python - 刪除列表項
- Python - 迴圈遍歷列表
- Python - 列表推導式
- Python - 排序列表
- Python - 複製列表
- Python - 合併列表
- Python - 列表方法
- Python - 列表練習
- Python 元組
- Python - 元組
- Python - 訪問元組項
- Python - 更新元組
- Python - 解包元組
- Python - 迴圈遍歷元組
- Python - 合併元組
- Python - 元組方法
- Python - 元組練習
- Python 集合
- Python - 集合
- Python - 訪問集合項
- Python - 新增集合項
- Python - 刪除集合項
- Python - 迴圈遍歷集合
- Python - 合併集合
- Python - 複製集合
- Python - 集合運算子
- Python - 集合方法
- Python - 集合練習
- Python 字典
- Python - 字典
- Python - 訪問字典項
- Python - 修改字典項
- Python - 新增字典項
- Python - 刪除字典項
- Python - 字典檢視物件
- Python - 迴圈遍歷字典
- Python - 複製字典
- Python - 巢狀字典
- Python - 字典方法
- Python - 字典練習
- Python 陣列
- Python - 陣列
- Python - 訪問陣列項
- Python - 新增陣列項
- Python - 刪除陣列項
- Python - 迴圈遍歷陣列
- Python - 複製陣列
- Python - 反轉陣列
- Python - 排序陣列
- Python - 合併陣列
- Python - 陣列方法
- Python - 陣列練習
- Python 檔案處理
- Python - 檔案處理
- Python - 寫入檔案
- Python - 讀取檔案
- Python - 重新命名和刪除檔案
- Python - 目錄
- Python - 檔案方法
- Python - OS 檔案/目錄方法
- Python - OS 路徑方法
- 面向物件程式設計
- Python - OOPs 概念
- Python - 類 & 物件
- Python - 類屬性
- Python - 類方法
- Python - 靜態方法
- Python - 建構函式
- Python - 訪問修飾符
- Python - 繼承
- Python - 多型
- Python - 方法重寫
- Python - 方法過載
- Python - 動態繫結
- Python - 動態型別
- Python - 抽象
- Python - 封裝
- Python - 介面
- Python - 包
- Python - 內部類
- Python - 匿名類和物件
- Python - 單例類
- Python - 包裝器類
- Python - 列舉
- Python - 反射
- Python 錯誤 & 異常
- Python - 語法錯誤
- Python - 異常
- Python - try-except 程式碼塊
- Python - try-finally 程式碼塊
- Python - 丟擲異常
- Python - 異常連結
- Python - 巢狀 try 程式碼塊
- Python - 使用者自定義異常
- Python - 日誌記錄
- Python - 斷言
- Python - 內建異常
- Python 多執行緒
- Python - 多執行緒
- Python - 執行緒生命週期
- Python - 建立執行緒
- Python - 啟動執行緒
- Python - 加入執行緒
- Python - 執行緒命名
- Python - 執行緒排程
- Python - 執行緒池
- Python - 主執行緒
- Python - 執行緒優先順序
- Python - 守護執行緒
- Python - 執行緒同步
- Python 同步
- Python - 執行緒間通訊
- Python - 執行緒死鎖
- Python - 中斷執行緒
- Python 網路
- Python - 網路
- Python - 套接字程式設計
- Python - URL 處理
- Python - 泛型
- Python 庫
- NumPy 教程
- Pandas 教程
- SciPy 教程
- Matplotlib 教程
- Django 教程
- OpenCV 教程
- Python 其他
- Python - 日期 & 時間
- Python - 數學
- Python - 迭代器
- Python - 生成器
- Python - 閉包
- Python - 裝飾器
- Python - 遞迴
- Python - 正則表示式
- Python - PIP
- Python - 資料庫訪問
- Python - 弱引用
- Python - 序列化
- Python - 模板
- Python - 輸出格式化
- Python - 效能測量
- Python - 資料壓縮
- Python - CGI 程式設計
- Python - XML 處理
- Python - GUI 程式設計
- Python - 命令列引數
- Python - 文件字串
- Python - JSON
- Python - 傳送郵件
- Python - 擴充套件
- Python - 工具/實用程式
- Python - GUI
- Python 高階概念
- Python - 抽象基類
- Python - 自定義異常
- Python - 高階函式
- Python - 物件內部
- Python - 記憶體管理
- Python - 元類
- Python - 使用元類進行超程式設計
- Python - 模擬和存根
- Python - 猴子補丁
- Python - 訊號處理
- Python - 型別提示
- Python - 自動化教程
- Python - Humanize 包
- Python - 上下文管理器
- Python - 協程
- Python - 描述符
- Python - 診斷和修復記憶體洩漏
- Python - 不可變資料結構
- Python 有用資源
- Python - 問答
- Python - 線上測驗
- Python - 快速指南
- Python - 參考
- Python - 速查表
- Python - 專案
- Python - 有用資源
- Python - 討論
- Python 編譯器
- NumPy 編譯器
- Matplotlib 編譯器
- SciPy 編譯器
使用 Python 建立語音錄音機
在這裡,我們將使用 Python 建立一個應用程式(語音錄音機),允許使用者錄製語音。我們將使用 sounddevice 庫和 Python 建立此語音錄音機應用程式。
使用語音錄音機,您可以啟動錄製過程、暫停它或在必要時取消。錄製的聲音儲存為兩種格式 WAV 和 WAVIO。此語音錄音機應用程式可用於只需要簡單音訊錄製和回放的應用程式。
所需庫
要在 Python 中建立語音錄音機,以下為所需的庫:
- sounddevice - sounddevice 庫將用於捕獲音訊。
- scipy - 此庫將用於使用 write 函式寫入 WAV 檔案。
- wavio - 此庫將用於使用其他選項寫入 wav 檔案。
- numpy - 此庫將用於矩陣或陣列計算。
安裝
要安裝建立語音記錄所需的庫,您需要使用 PIP 安裝這些庫。使用以下語句一次安裝所有所需的庫:
pip install sounddevice scipy wavio numpy
建立語音錄音機的步驟
1. 匯入庫
安裝完成後,您需要在程式碼中匯入所有庫。要匯入所需的庫,請使用以下程式碼語句:
import sounddevice as sd from scipy.io.wavfile import write import wavio as wv import numpy as np import threading
步驟 2:初始化變數
使用以下程式碼設定取樣頻率和用於管理錄製狀態的標誌:
- 設定取樣頻率:
freq = 44100
- 儲存錄製的音訊片段:
recording = []
- 設定預設標誌值以檢查錄製是否處於活動狀態:
is_recording = False
- 設定預設標誌值以停止錄製:
stop_recording = False
步驟 3:定義錄製函式
建立一個函式來處理迴圈中的音訊錄製,並將每個片段追加到錄製列表中。使用以下程式碼:
def record_audio(): global recording, is_recording, stop_recording is_recording = True while is_recording and not stop_recording: recording_segment = sd.rec(int(freq), samplerate=freq, channels=2) sd.wait() # Wait until the recording is finished recording.append(recording_segment)
步驟 4:定義輸入處理函式
實現一個函式來處理用於錄製控制(開始、暫停、停止)的使用者命令。使用以下程式碼:
def handle_input(): global is_recording, stop_recording while True: user_input = input("r to START , p to PAUSE and s to STOP the recording, please choice accordingly \n Enter command: ").lower() if user_input == 'p': if is_recording: print("Recording Paused, (you can again start the recording using 'r')") is_recording = False else: print("Recording is not active.") elif user_input == 'r': if not is_recording: print("Recording Resumed (type 'p' to pause or 's' to stop)") threading.Thread(target=record_audio).start() else: print("Already recording.") elif user_input == 's': if is_recording: print("Stopping recording. Thank you for recording :)") is_recording = False stop_recording = True break else: print("Nothing to stop.")
步驟 5:組合並保存錄制
停止錄製後,將所有錄製片段組合成一個音訊檔案,並將其儲存為不同的格式。使用以下程式碼:
if recording: final_recording = np.concatenate(recording, axis=0) write("recording0.wav", freq, final_recording) wv.write("recording1.wav", final_recording, freq, sampwidth=2) print("Recording saved as 'recording0.wav' and 'recording1.wav'.") else: print("No recording was made.")
建立語音錄音機的 Python 程式碼
以下是使用 Python 建立語音錄音機應用程式的完整程式碼:
import sounddevice as sd from scipy.io.wavfile import write import wavio as wv import numpy as np import threading # Sampling frequency freq = 44100 # Initialize recording variables recording = [] is_recording = False stop_recording = False def record_audio(): global recording, is_recording, stop_recording is_recording = True while is_recording and not stop_recording: recording_segment = sd.rec(int(freq), samplerate=freq, channels=2) sd.wait() recording.append(recording_segment) def handle_input(): global is_recording, stop_recording while True: user_input = input("r to START , p to PAUSE and s to STOP the recording, please choice accordingly \n Enter command: ").lower() if user_input == 'p': if is_recording: print("Recording Paused, (you can again start the recording using 's') ") is_recording = False else: print("Recording is not working activly.") elif user_input == 'r': if not is_recording: print("Recording Resumed (type 'p' to pause or 's' to stop)") threading.Thread(target=record_audio).start() else: print("Already recording.") elif user_input == 's': if is_recording: print("Stopping recording. Thank you for recording :) ") is_recording = False stop_recording = True break else: print(" Nothing to stop.") # Start the input handler in a separate thread input_thread = threading.Thread(target=handle_input) input_thread.start() # Wait for the input thread to complete input_thread.join() # Combine all the recorded segments if recording: final_recording = np.concatenate(recording, axis=0) write("recording0.wav", freq, final_recording) wv.write("recording1.wav", final_recording, freq, sampwidth=2) print("Recording saved as 'recording0.wav' and 'recording1.wav'.") else: print("No recording was made.")
輸出
執行此程式碼後,您將看到按下“r”鍵開始錄製,“p”鍵暫停,“s”鍵停止錄製。

現在您還可以在您的電腦上看到錄製的音訊檔案。

之後,您就可以成功收聽您的音訊檔案了。

程式碼解釋
- **匯入庫** - 匯入用於錄製和處理音訊以及管理音訊檔案的庫。
- **初始化變數** - 應設定“取樣率”、“錄製列表”和“標誌”作為錄製控制引數。
- **定義 record_audio 函式** - 錄製音訊片段並將其新增到列表中。
- **定義 handle_input 函式** - 操作使用者開始或停止錄製過程的指令。
- **啟動輸入處理執行緒** - 線上程中管理輸入處理函式,以避免遊戲凍結。
- **等待輸入執行緒完成** - 在繼續執行主執行緒的後續操作之前,等待輸入執行緒完成執行。
- **合併錄製片段** - 將所有錄製的音訊片段組合成一個軌道或稱為片段。
- **以 WAV 格式保存錄制** - 透過兩種方法將組合後的音訊儲存為 WAV 檔案。
- **列印完成訊息** - 讓使用者知道錄製已成功儲存。
- **處理無錄製場景** - 如果未錄製任何音訊,則發出警報。
python_reference.htm
廣告