午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術(shù)文章
文章詳情頁

基于Python 的語音重采樣函數(shù)解析

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

因?yàn)楣ぷ髦袝?huì)經(jīng)常遇到不同采樣率的聲音文件的問題,特意寫了一下重采樣的程序。

原理就是把采樣點(diǎn)轉(zhuǎn)換到時(shí)間刻度之后再進(jìn)行插值,經(jīng)過測試,是沒有問題的。

#!/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: lccever@126.comimport 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)

補(bǔ)充知識:Python 多線程的退出/停止的一種是實(shí)現(xiàn)思路

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

一個(gè)比較合理的方式就是把原因需要放到threading.Thread的target中的線程函數(shù),改寫到一個(gè)繼承類中,下面是一個(gè)實(shí)現(xiàn)例子

import threadingimport timeimport os # 原本需要用來啟動(dòng)的無線循環(huán)的函數(shù)def print_thread(): pid = os.getpid() counts = 0 while True: print(f’threading pid: {pid} ran: {counts:04d} s’) counts += 1 time.sleep(1) # 把函數(shù)放到改寫到類的run方法中,便可以通過調(diào)用類方法,實(shí)現(xiàn)線程的終止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) # 主動(dòng)把線程退出 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 的語音重采樣函數(shù)解析就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 91免费进入 | 草草草在线 | 亚洲成年人网 | 午夜av一区二区 | 超碰在线中文 | 一级在线观看视频 | 国产又黄又爽免费视频 | av在线官网 | 国产福利精品在线 | 天堂在线免费观看视频 | 成人亚洲网| 麻豆免费av| 国产成人精品一区二区 | 综合亚洲精品 | 天天干天天干 | 欧美小视频在线观看 | 精品国产一区在线 | 欧美一级免费大片 | 黄色成年视频 | 欧美成人三区 | 黑人巨大精品欧美一区二区 | 欧美特黄aaaaaa| 亚洲精选av| 精品视频一区二区三区四区 | 日本女人裸体视频 | 婷婷影视 | 最新99热| 青青青久久久 | 老鸭窝毛片 | 麻豆国产精品 | 91精品久久久久久久久久久久 | 中文字幕视频网站 | av导航网 | 国产黄色免费观看 | 九九视频免费观看 | 在线观看美女av | 国产综合网站 | 国偷自拍第113页 | 午夜影院h | 国产成人在线精品 | 久久久久久久久久国产 |