欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

原理就是把采樣點轉(zhuǎ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.com
import numpy as np
def 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_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)

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

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

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

import threading
import time
import os
 
# 原本需要用來啟動的無線循環(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)用類方法,實現(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)
 # 主動把線程退出
 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)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python數(shù)據(jù)類型學(xué)習(xí)筆記

    Python數(shù)據(jù)類型學(xué)習(xí)筆記

    這篇文章主要針對Python數(shù)據(jù)類型為大家進行了詳細介紹,整理一篇關(guān)于Python數(shù)據(jù)類型的學(xué)習(xí)筆記,感興趣的小伙伴們可以參考一下
    2016-01-01
  • Pythony運維入門之Socket網(wǎng)絡(luò)編程詳解

    Pythony運維入門之Socket網(wǎng)絡(luò)編程詳解

    這篇文章主要介紹了Pythony運維入門之Socket網(wǎng)絡(luò)編程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • NumPy實現(xiàn)結(jié)構(gòu)化數(shù)組的示例代碼

    NumPy實現(xiàn)結(jié)構(gòu)化數(shù)組的示例代碼

    結(jié)構(gòu)化數(shù)組是 NumPy 中用于處理異質(zhì)數(shù)據(jù)的重要工具,通過定義復(fù)雜的數(shù)據(jù)類型,我們可以創(chuàng)建具有不同字段的數(shù)組,本文主要介紹了NumPy實現(xiàn)結(jié)構(gòu)化數(shù)組的示例代碼,具有一定的參考價值,感興趣的可以了解一下
    2024-01-01
  • python使用opencv在Windows下調(diào)用攝像頭實現(xiàn)解析

    python使用opencv在Windows下調(diào)用攝像頭實現(xiàn)解析

    這篇文章主要介紹了python使用opencv在Windows下調(diào)用攝像頭實現(xiàn)解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • python仿抖音表白神器

    python仿抖音表白神器

    這篇文章主要教大家制作python抖音表白神器,仿制抖音表白小軟件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • Python不規(guī)范的日期字符串處理類

    Python不規(guī)范的日期字符串處理類

    這篇文章主要介紹了Python不規(guī)范的日期字符串處理類,可以對一些非正規(guī)的日期字符串進行解析、轉(zhuǎn)換、比較等,需要的朋友可以參考下
    2014-06-06
  • pandas數(shù)據(jù)處理基礎(chǔ)之篩選指定行或者指定列的數(shù)據(jù)

    pandas數(shù)據(jù)處理基礎(chǔ)之篩選指定行或者指定列的數(shù)據(jù)

    這篇文章主要介紹了pandas數(shù)據(jù)處理基礎(chǔ)之篩選指定行或者指定列的數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下
    2018-05-05
  • Python雙版本計算器詳解

    Python雙版本計算器詳解

    這篇文章主要介紹了如何用Python制作計算器,文章中有兩個版本,代碼詳細,適合絕大部分朋友,如果你對python如何制作計算器有興趣,可以參考下這篇文章
    2021-04-04
  • python調(diào)用機器喇叭發(fā)出蜂鳴聲(Beep)的方法

    python調(diào)用機器喇叭發(fā)出蜂鳴聲(Beep)的方法

    這篇文章主要介紹了python調(diào)用機器喇叭發(fā)出蜂鳴聲(Beep)的方法,實例分析了Python調(diào)用winsound模塊的使用技巧,需要的朋友可以參考下
    2015-03-03
  • 如何利用python進行時間序列分析

    如何利用python進行時間序列分析

    這篇文章主要介紹了如何利用python進行時間序列分析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08

最新評論