国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

基于Python 的語音重采樣函數解析

瀏覽:51日期:2022-07-18 14:52:18

因為工作中會經常遇到不同采樣率的聲音文件的問題,特意寫了一下重采樣的程序。

原理就是把采樣點轉換到時間刻度之后再進行插值,經過測試,是沒有問題的。

#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 17-7-21 下午2:32# @Author : Lei.Jinggui# @Site : http://blog.csdn.net/lccever# @File : Resample.py# @Software: PyCharm Community Edition# @contact: [email protected] numpy as npdef Resample(input_signal,src_fs,tar_fs): ’’’ :param input_signal:輸入信號 :param src_fs:輸入信號采樣率 :param tar_fs:輸出信號采樣率 :return:輸出信號 ’’’ dtype = input_signal.dtype audio_len = len(input_signal) audio_time_max = 1.0*(audio_len-1) / src_fs src_time = 1.0 * np.linspace(0,audio_len,audio_len) / src_fs tar_time = 1.0 * np.linspace(0,np.int(audio_time_max*tar_fs),np.int(audio_time_max*tar_fs)) / tar_fs output_signal = np.interp(tar_time,src_time,input_signal).astype(dtype) return output_signalif __name__ == ’__main__’: import wave import pyaudio def playSound(audio_data_short, framerate=16000, channels=1): preply = pyaudio.PyAudio() # 播放聲音 streamreply = preply.open(format=pyaudio.paInt16, channels=channels, rate=framerate, output=True) data = audio_data_short.tostring() streamreply.write(data) streamreply.close() preply.terminate() wave_file = ’test.wav’ audio_file = wave.open(wave_file, ’rb’) audio_data = audio_file.readframes(audio_file.getnframes()) audio_data_short = np.fromstring(audio_data, np.short) src_fs = audio_file.getframerate() src_chanels = audio_file.getnchannels() if src_chanels > 1: audio_data_short = audio_data_short[::src_chanels] tar_fs = np.int(src_fs * 0.5) playSound(audio_data_short,framerate=src_fs) audio_data_short0 = Resample(audio_data_short,src_fs,tar_fs) playSound(audio_data_short0,framerate=tar_fs)

補充知識:Python 多線程的退出/停止的一種是實現思路

在使用多線程的過程中,我們知道,python的線程是沒有stop/terminate方法的,也就是說它被啟動后,你無法再主動去退出它,除非主進程退出了,注意,是主進程,不是線程的父進程.

一個比較合理的方式就是把原因需要放到threading.Thread的target中的線程函數,改寫到一個繼承類中,下面是一個實現例子

import threadingimport timeimport os # 原本需要用來啟動的無線循環的函數def print_thread(): pid = os.getpid() counts = 0 while True: print(f’threading pid: {pid} ran: {counts:04d} s’) counts += 1 time.sleep(1) # 把函數放到改寫到類的run方法中,便可以通過調用類方法,實現線程的終止class StoppableThread(threading.Thread): def __init__(self, daemon=None): super(StoppableThread, self).__init__(daemon=daemon) self.__is_running = True self.daemon = daemon def terminate(self): self.__is_running = False def run(self): pid = os.getpid() counts = 0 while self.__is_running: print(f’threading running: {pid} ran: {counts:04d} s’) counts += 1 time.sleep(1) def call_thread(): thread = StoppableThread() thread.daemon = True thread.start() pid = os.getpid() counts = 0 for i in range(5): print(f’0 call threading pid: {pid} ran: {counts:04d} s’) counts += 2 time.sleep(2) # 主動把線程退出 thread.terminate() if __name__ == ’__main__’: call_thread() print(f’==========call_thread finish===========’) counts = 0 for i in range(5): counts += 1 time.sleep(1) print(f’main thread:{counts:04d} s’)

以上這篇基于Python 的語音重采樣函數解析就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 精品欧美一区二区在线观看 | 九九国产精品九九 | 在线免费成人网 | 成人交性视频免费看 | 日韩一区二区三区视频 | 高清偷自拍第1页 | 亚洲手机在线观看 | 亚洲一区二区三区精品影院 | 亚洲加勒比久久88色综合一区 | 在线观看国产 | 久在线| 国产17部性孕妇孕交在线 | 亚洲自拍小视频 | 亚洲欧美中文日韩在线v日本 | 免费一级大毛片a一观看不卡 | 人成精品 | 国产一区不卡 | 免费在线观看毛片 | 国产日韩亚洲欧美 | 午夜日本一区二区三区 | 国产欧美成人不卡视频 | 日本农村寡妇一级毛片 | 男女免费观看在线爽爽爽视频 | 国产精品第五页 | 中文字幕一区二区在线视频 | 美女脱了内裤张开腿让男人桶网站 | 国产不卡精品一区二区三区 | 国产高清亚洲 | 一级毛片免费观看视频 | 亚洲人成网址在线观看 | 日韩欧美一区二区精品久久 | 国产美女动态免费视频 | 欧美一级在线视频 | 亚洲精品视频免费看 | 国产欧美日韩不卡在线播放在线 | 国产欧美日韩综合精品无毒 | 免费国产成人18在线观看 | 亚洲精品国产专区91在线 | 国产精品亚洲四区在线观看 | 一本本久综合久久爱 | 国内欧美一区二区三区 |