I have a .wav file several minutes long that I would like to split into different 10 second .wav files.
This is my python code so far:
import wave
import math
def main(filename, time):
read = wave.open(filename, 'r')
#get sample rate
frameRate = read.getframerate()
#get number of frames
numFrames = read.getnframes()
#get duration
duration = numFrames/frameRate
#get all frames as a string of bytes
frames = read.readframes(numFrames)
#get 1 frame as a string of bytes
oneFrame = read.readframes(1)
#framerate*time == numframesneeded
numFramesNeeded=frameRate*time
#numFramesNeeded*oneFrame=numBytes
numBytes = numFramesNeeded*oneFrame
#splice frames to get a list strings each representing a 'time' length
#wav file
x=0
wavList=[]
while x+time<=duration:
curFrame= frames[x:x+time]
x=x+time
wavList.append(curFrame)
Printing wavList yields:
['\x00\x00\x00\x00\xff\xff\x00\x00\x00\x00', '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00']
I know that this is a list of frames. How do I make one wav file for each element in this list (the first .wav file would be '\x00\x00\x00\x00\xff\xff\x00\x00\x00\x00'? Python's wave module is unclear about using frames to create .wav files.
EDIT: This is a duplicate question of How to splice an audio file (wav format) into 1 sec splices in python?
However, if someone has an answer that does not require pydub I would very much like to see it.
pydubis obviously the straightforward way to do this task. It seems to me that what you're really asking for is code review. If you're asking about how to do the think at a lower level, why not just read thepydubcode ? e.g. seeget_sample_sliceand the manipulations of._datamanipulations in general. - Nikana Reklawyks