4

I want to write a python program that write chunks from an audio file. I can write chunks from an audio file available locally using following code,

from pydub import AudioSegment
from pydub.utils import make_chunks

myaudio = AudioSegment.from_file("file1.wav" , "wav") 
chunk_length_ms = 10000 # pydub calculates in millisec
chunks = make_chunks(myaudio, chunk_length_ms) #Make chunks of one sec

#Export all of the individual chunks as wav files

for i, chunk in enumerate(chunks):
    chunk_name = "chunk{0}.wav".format(i)
    print "exporting", chunk_name
    chunk.export(chunk_name, format="wav")

The above code will create chunks with 10000 milliseconds of the audio file "file1.wav". But I want to write chunks from an audio stream, the stream could be wav or mp3. Can someone help me on this?

user867662
  • 1,091
  • 4
  • 20
  • 45

1 Answers1

3

change the audio chunk to numpy array and use the function .get_array_of_samples()

np.array(chunk[0].get_array_of_samples())
Santhosh s
  • 184
  • 1
  • 5