最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年07月06日 14:27:11   作者:InterStellar1145  
這篇文章主要介紹了基于Python 的語(yǔ)音重采樣函數(shù)解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

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

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

#!/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.com
import numpy as np
def Resample(input_signal,src_fs,tar_fs):
 '''
 :param input_signal:輸入信號(hào)
 :param src_fs:輸入信號(hào)采樣率
 :param tar_fs:輸出信號(hào)采樣率
 :return:輸出信號(hào)
 '''
 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_signal

if __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ǔ)充知識(shí):Python 多線程的退出/停止的一種是實(shí)現(xiàn)思路

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

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

import threading
import time
import os
 
# 原本需要用來(lái)啟動(dòng)的無(wú)線循環(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方法中,便可以通過(guò)調(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 的語(yǔ)音重采樣函數(shù)解析就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python使用 zip 同時(shí)迭代多個(gè)序列示例

    python使用 zip 同時(shí)迭代多個(gè)序列示例

    這篇文章主要介紹了python使用 zip 同時(shí)迭代多個(gè)序列,結(jié)合實(shí)例形式分析了Python使用zip遍歷迭代長(zhǎng)度相等與不等的序列相關(guān)操作技巧,需要的朋友可以參考下
    2019-07-07
  • Python的pywifi無(wú)線網(wǎng)絡(luò)庫(kù)的具體使用

    Python的pywifi無(wú)線網(wǎng)絡(luò)庫(kù)的具體使用

    pywifi是一個(gè)基于Python的用于操作無(wú)線網(wǎng)絡(luò)的庫(kù),本文就來(lái)介紹一下pywifi的安裝及實(shí)際應(yīng)用場(chǎng)景使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02
  • python pandas cumsum求累計(jì)次數(shù)的用法

    python pandas cumsum求累計(jì)次數(shù)的用法

    這篇文章主要介紹了python pandas cumsum求累計(jì)次數(shù)的用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • 基于TensorFlow常量、序列以及隨機(jī)值生成實(shí)例

    基于TensorFlow常量、序列以及隨機(jī)值生成實(shí)例

    今天小編就為大家分享一篇基于TensorFlow常量、序列以及隨機(jī)值生成實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-01-01
  • Python基于TensorFlow接口實(shí)現(xiàn)深度學(xué)習(xí)神經(jīng)網(wǎng)絡(luò)回歸

    Python基于TensorFlow接口實(shí)現(xiàn)深度學(xué)習(xí)神經(jīng)網(wǎng)絡(luò)回歸

    這篇文章主要為大家詳細(xì)介紹了如何基于Python語(yǔ)言中TensorFlow的tf.estimator接口,實(shí)現(xiàn)深度學(xué)習(xí)神經(jīng)網(wǎng)絡(luò)回歸的具體方法,感興趣的可以了解一下
    2023-02-02
  • numpy稀疏矩陣的實(shí)現(xiàn)

    numpy稀疏矩陣的實(shí)現(xiàn)

    本文主要介紹了numpy稀疏矩陣的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • numpy和tensorflow中的各種乘法(點(diǎn)乘和矩陣乘)

    numpy和tensorflow中的各種乘法(點(diǎn)乘和矩陣乘)

    這篇文章主要介紹了numpy和tensorflow中的各種乘法(點(diǎn)乘和矩陣乘),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • python3將變量輸入的簡(jiǎn)單實(shí)例

    python3將變量輸入的簡(jiǎn)單實(shí)例

    在本篇文章里小編給大家整理的是一篇關(guān)于python3將變量輸入的簡(jiǎn)單實(shí)例內(nèi)容,有需要的朋友們可以參考下。
    2020-08-08
  • python工廠方法模式原理與實(shí)現(xiàn)

    python工廠方法模式原理與實(shí)現(xiàn)

    大家好,本篇文章主要講的是python工廠方法模式原理與實(shí)現(xiàn),感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-02-02
  • Python3爬樓梯算法示例

    Python3爬樓梯算法示例

    這篇文章主要介紹了Python3爬樓梯算法,涉及Python基于面向?qū)ο蟮淖址闅v、切片、運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下
    2019-03-03

最新評(píng)論

射阳县| 蒙自县| 麻阳| 中牟县| 高唐县| 新平| 上思县| 阳东县| 色达县| 呈贡县| 姜堰市| 日土县| 新津县| 康平县| 家居| 晋城| 东丰县| 云梦县| 全椒县| 乌苏市| 木兰县| 晋宁县| 屏山县| 闸北区| 泰宁县| 尤溪县| 丹凤县| 永城市| 石林| 丰顺县| 密云县| 公安县| 商水县| 神木县| 宜良县| 万全县| 克山县| 张家界市| 肃南| 民县| 林甸县|