My goal is to overwrite the wav extension file into a binary file (binary file / file.bin). For coding, I use quantization theory (creating a quantization step ....).
The code implements the encoding function. The elements that change are the track itself, the number of bits and the target (that is, in which file I write, in my case it is bin).
import scipy.io.wavfile as wav import numpy as np import pickle def enc(track, n, target): rate, data = wav.read(track) qStep = (float(np.max(data)) - float(np.min(data)))/(2**n-1) dataQuant = np.round(data/(qStep))*qStep b = open(target, 'wb') pickle.dump(data, b, pickle.HIGHEST_PROTOCOL) b.close() If I run it as: enc('track.wav', 16, 'enс.bin') . I get a binary file that is similar in size with the track, which I think is true, since the track is 16 bits.
If I run: enc('track.wav', 8, 'enс8.bin') , i.e. if I want to transcode to 8 bits, I get a file that is similar in size to en. Bin, although it should be 2 times smaller than size.
I use the same track.
How do I fix my mistake?
New task
I want to overwrite my file back, that is, from the bin format to wav. For this I prescribe:
b = open(filename,"rb") data= pickle.load(b) b.close() scipy.io.wavfile.write(str.replace(filename,".bin","_decoded.wav"), 44100, data) I get a "terrible" track when decoding from 8 bits of a file. How to fix?
scaled =np.array(data/255,dtype='int8')- LenaPark