0

How can I:

  • read my audio file
  • stores in a binary file,

Can somebody give me examples to implement encoder and decoder in python?

1 Answers1

2

You can use scipy.wave to read and write the wav file. To store the data, you could use numpy.

If the audio file is effectively encoded with 16 bits per sample, you don't have to do anything and this should will something like:

 from scipy.io.wavfile import read as wavread
 from scipy.io.wavfile import write as wavwrite
 import numpy as np

 sr, sig = wavread(audioFileName) #read the audio file (samplig rate, signal)
 sig_int8 = np.uint8(sig) # cast the data in uint8
 np.savez(out_file, sig = sig_int8) # store the data

 npzfile = np.load(out_file + '.npz') #load the data
 sig = npzfile['sig']

 wavwrite(audioFileName2, sr, sig) #write data in wav file
PatriceG
  • 3,851
  • 5
  • 28
  • 43
  • If i need quantize the audio samples from the sound file to 8 bit per sample, what i should change in your program? –  Oct 22 '17 at 08:11
  • In scipy write, there is a rate option. See here: https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.io.wavfile.write.html – PatriceG Oct 23 '17 at 08:07
  • i wrote smt like : wavwrite(file name, sr,'uint8') anf got the error: 'str' object has no attribute 'dtype'. –  Oct 23 '17 at 16:06
  • I edited the answer – PatriceG Oct 24 '17 at 07:17