Read Wav File From Python And Convert Frames Into Raw S16le String
I'm reading a file in Python using either of librosa, wave or soundfile libraries and I need to push the chunk (any size) to a HTTP stream. By specification, stream string input re
Solution 1:
You can try pydub to convert audio to audio-segment, split audio-segment into chunks that are playable (i.e you can play each chunk), then convert them to raw as needed.
Here is a quick code.
from pydub import AudioSegment
from pydub.utils import make_chunks
myaudio = AudioSegment.from_file("myaudio.wav" , "wav")
chunk_length_ms = 1000# pydub calculates in millisec
chunks = make_chunks(myaudio, chunk_length_ms) #Make chunks of one sec#Convert chunks to raw audio data which you can then feed to HTTP streamfor i, chunk inenumerate(chunks):
raw_audio_data = chunk.raw_data
By default raw audio is 16bit
>>>
bytes_per_sample= 2 # 2 byte (16 bit) samples
Since raw_audio_data
is raw, if above format doesn't work, you can convert to any other format as needed. Check pydub utils api for details.
Solution 2:
If you want to get raw data from a wav file in one go, then pydub can also be used like this:
from pydub import AudioSegment
sound = AudioSegment.from_wav('your_audio.wav') # can do same for mp3 and other formats
raw = sound._data # returns byte string print(raw) # prints "b'k\xffe\xffw\xff\x83\xffu\xff\x85\xff\x81\xff\x85\xff\xa5....."
Post a Comment for "Read Wav File From Python And Convert Frames Into Raw S16le String"