python - 将音频信号分成小样本

标签 python audio

我正在尝试处理音频信号并将音频信号分成N个离散样本,然后我想独立播放这些样本。

如何使用python做到这一点?

最佳答案

 import wave
 import pygame
 import time



def slice(infile, outfilename, start_ms, end_ms):
    width = infile.getsampwidth() #Returns sample width in bytes
    rate = infile.getframerate()  #Returns sampling frequency
    fpms = rate / 1000 # frames per ms
    length = (end_ms - start_ms) * fpms
    start_index = start_ms * fpms

    out = wave.open(outfilename, "w")
    out.setparams((infile.getnchannels(), width, rate, length, infile.getcomptype(), infile.getcompname()))

    infile.rewind()             #Rewind the file pointer to the beginning of the audio stream
    anchor = infile.tell()      #Return current file pointer position
    infile.setpos(anchor + start_index)  #Set the file pointer to the specified position
    out.writeframes(infile.readframes(length)) #Write audio frames and make sure nframes is correct


if __name__ == "__main__":

     slice(wave.open("song1.wav", "r"), "out.wav", 500, 5000)

     pygame.mixer.init()
     pygame.mixer.music.load("out.wav")
     pygame.mixer.music.play()
     while pygame.mixer.music.get_busy() == True:
           continue

关于python - 将音频信号分成小样本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33451325/

相关文章:

iphone - AVAudioPlayer 的外部 URL 为 *.m4p

iphone - 如何使用simpleaudioengine播放指定时长的音频

python - 过度使用 sitecustomize.py : . bash_profile 来代替?

python - Pandas groupby 在多列中获取最大值行

python |类型错误 : 'float' object cannot be interpreted as an integer

python - 比较文件 OpenCV 中的多个直方图

java - 列出 Android 中所有可用的通知铃声

python - 使按钮在第二次按下时表现不同 (Python)

javascript - 试图让音频先开始播放

python - python中的多处理嵌套数值积分