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

Python對wav文件的重采樣實例

 更新時間:2020年02月25日 10:04:31   作者:AYE89  
今天小編就為大家分享一篇Python對wav文件的重采樣實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

例如從2channel,4.41k hz 重采樣到 1 channel,16k hz

def downsampleWav(src, dst, inrate=44100, outrate=16000, inchannels=2, outchannels=1):
 import os,wave,audioop
 if not os.path.exists(src):
  print ('Source not found!')
  return False
 
 if not os.path.exists(os.path.dirname(dst)):
  os.makedirs(os.path.dirname(dst))
 
 try:
  s_read = wave.open(src, 'r')
  s_write = wave.open(dst, 'w')
 except:
  print ('Failed to open files!')
  return False
 
 n_frames = s_read.getnframes()
 data = s_read.readframes(n_frames)
 
 try:
  converted = audioop.ratecv(data, 2, inchannels, inrate, outrate, None)
  if outchannels == 1:
   converted = audioop.tomono(converted[0], 2, 1, 0)
 except:
  print ('Failed to downsample wav')
  return False
 
 try:
  s_write.setparams((outchannels, 2, outrate, 0, 'NONE', 'Uncompressed'))
  s_write.writeframes(converted)
 except:
  print ('Failed to write wav')
  return False
 
 try:
  s_read.close()
  s_write.close()
 except:
  print ('Failed to close wav files')
  return False
 
 return True
 

若in和out都是單通道:

def downsampleWav(src, dst, inrate=48000, outrate=16000, inchannels=1, outchannels=1):
 import os,wave,audioop
 if not os.path.exists(src):
  print ('Source not found!')
  return False
 
 if not os.path.exists(os.path.dirname(dst)):
  os.makedirs(os.path.dirname(dst))
 
 try:
  s_read = wave.open(src, 'rb')
  params = s_read.getparams()
  nchannels, sampwidth, framerate, nframes = params[:4]
  print(nchannels,sampwidth, framerate,nframes)
  s_write = wave.open(dst, 'wb')
 except:
  print ('Failed to open files!')
  return False
 
 n_frames = s_read.getnframes()
 data = s_read.readframes(n_frames)
 
 try:
  converted = audioop.ratecv(data, 2, inchannels, inrate, outrate, None)
  if outchannels == 1 and inchannels != 1:
   converted = audioop.tomono(converted[0], 2, 1, 0)
 except:
  print ('Failed to downsample wav')
  return False
 
 try:
  s_write.setparams((outchannels, 2, outrate, 0, 'NONE', 'Uncompressed'))
  s_write.writeframes(converted[0])
 except Exception as e:
  print(e)
  print ('Failed to write wav')
  return False
 
 try:
  s_read.close()
  s_write.close()
 except:
  print ('Failed to close wav files')
  return False
 
 return True

方案二

y為下采樣的結(jié)果,類型np.ndarray

You can use Librosa's load() function,

import librosa
y, s = librosa.load('test.wav', sr=8000) # Downsample 44.1kHz to 8kHz

The extra effort to install Librosa is probably worth the peace of mind.

Pro-tip: when installing Librosa on Anaconda, you need to install ffmpeg as well, so

pip install librosa
conda install -c conda-forge ffmpeg

以上這篇Python對wav文件的重采樣實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python之PyQt6對話框的實現(xiàn)

    Python之PyQt6對話框的實現(xiàn)

    這篇文章主要介紹了Python之PyQt6對話框的實現(xiàn),文章內(nèi)容詳細,簡單易懂,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2023-01-01
  • django接入新浪微博OAuth的方法

    django接入新浪微博OAuth的方法

    這篇文章主要介紹了django接入新浪微博OAuth的方法,實例分析了django針對新浪微博OAuth接口的接入技巧,需要的朋友可以參考下
    2015-06-06
  • matlab繪制局部放大圖圖文教程

    matlab繪制局部放大圖圖文教程

    這篇文章主要給大家介紹了關(guān)于matlab繪制局部放大圖的相關(guān)資料,所謂局部放大即呈現(xiàn)子圖,以顯示局部細節(jié),需要的朋友可以參考下
    2023-07-07
  • python識別文字(基于tesseract)代碼實例

    python識別文字(基于tesseract)代碼實例

    這篇文章主要介紹了python識別文字(基于tesseract)代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08
  • TensorFlow用expand_dim()來增加維度的方法

    TensorFlow用expand_dim()來增加維度的方法

    今天小編就為大家分享一篇TensorFlow用expand_dim()來增加維度的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Python實現(xiàn).gif圖片拆分為.png圖片的簡單示例

    Python實現(xiàn).gif圖片拆分為.png圖片的簡單示例

    有時候需要把GIF圖片分解成一張一張的靜態(tài)圖,jpg或者png格式,下面這篇文章主要給大家介紹了關(guān)于Python實現(xiàn).gif圖片拆分為.png圖片的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • 在pycharm中為項目導(dǎo)入anacodna環(huán)境的操作方法

    在pycharm中為項目導(dǎo)入anacodna環(huán)境的操作方法

    這篇文章主要介紹了在pycharm中為項目導(dǎo)入anacodna環(huán)境的操作方法,本文圖文并茂通過實例詳解的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • Python命名空間及作用域原理實例解析

    Python命名空間及作用域原理實例解析

    這篇文章主要介紹了Python命名空間及作用域原理實例解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-08-08
  • Python中時間類型的JSON數(shù)據(jù)轉(zhuǎn)換

    Python中時間類型的JSON數(shù)據(jù)轉(zhuǎn)換

    在Python中,處理時間和日期數(shù)據(jù)以及與JSON數(shù)據(jù)的相互轉(zhuǎn)換是常見的任務(wù),本文主要為大家詳細如何在Python中處理時間類型的JSON數(shù)據(jù)轉(zhuǎn)換,需要的小伙伴可以參考下
    2024-02-02
  • 8種常用的Python工具

    8種常用的Python工具

    這篇文章主要介紹了8種常用的Python工具,幫助大家更好的學(xué)習(xí)Python,感興趣的朋友可以了解下
    2020-08-08

最新評論

万源市| 长泰县| 安岳县| 正安县| 鹿邑县| 卓尼县| 安西县| 拉孜县| 武强县| 青阳县| 马龙县| 贞丰县| 榆社县| 连南| 邯郸县| 宽城| 清苑县| 铜鼓县| 大名县| 桐梓县| 安远县| 屏东县| 北流市| 新兴县| 石台县| 谢通门县| 商都县| 牙克石市| 万盛区| 苏尼特左旗| 三门峡市| 屏山县| 崇明县| 习水县| 集贤县| 宁乡县| 东阳市| 邓州市| 海林市| 海原县| 财经|