convert Wav file to binary, send it through simulink and convert it back to wav
Mostra commenti meno recenti
Hi,
i want to convert a .wav file (16Bit, 44100Hz, stereo) to a binary stream file, than send it through simulink via "Binary File Reader", read the Output from "Binary File Writer" back into Matlab and convert it back to .wav to hear the result of my Reverb i build with simulink.
my steps so far:
wavdata = audioread("Closed-Hi-Hat-1.wav");
wavbinary = dec2bin( typecast( single(wavdata(:)), 'uint8'), 8 ) - '0';
now i have a wavdata variable with value:3247x2 double
and a wavbinary with 25976x8 double
fid = fopen('wav.bin', 'w');
fwrite(fid, wavbinary);
fclose(fid);
I now have the "wav.bin" file i can add to my "Binary File Reader" in simulink, when i send it through my "Reverb" i get a new "wav_reverb.bin" file.
next steps back in matlab:
fid = fopen('wav_reverb.bin');
A = fread(fid);
sound(A);
when i now play (A) it sounds not like the actual "Closed-Hi-Hat-1.wav" i sent through my "Reverb". It sounds very bad and scratchy. I think its a problem with the data types. And i dont know how to convert the "wav_reverb.bin" back to a .wav (maybe it sounds better than).
PS: i tried the example from Walter
>> wavbinary = dec2bin( typecast( single(wavdata(:)), 'uint8'), 8 ) - '0';
>> orig_size = size(wavdata);
>> data_class_to_use = 'int32';
>> SampleRate = 22100;
>> wavdata = reshape( typecast( uint8(bin2dec( char(wavbinary + '0') )), data_class_to_use ), orig_size );
>> audiowrite('FileNameGoesHer33e.wav', wavdata, SampleRate)
but the new audiofile sounds also scratchy and bad.
I hope anyone can help me
thanks
LK
3 Commenti
Walter Roberson
il 5 Lug 2019
You need to reshape A into 8 columns and uint8 that and typecast to single.
Note: I suggest that you look at the audioreader option 'native' which will make it easier for you.
Lukas Kuhl
il 5 Lug 2019
Modificato: Lukas Kuhl
il 5 Lug 2019
Lukas Kuhl
il 5 Lug 2019
Modificato: Lukas Kuhl
il 5 Lug 2019
Risposta accettata
Più risposte (2)
Walter Roberson
il 5 Lug 2019
wavdata = audioread("Closed-Hi-Hat-1.wav", 'native');
wavbinary = uint8(reshape((dec2bin( wavdata(:), 8 ) - '0').', [], 1));
fid = fopen('wav.bin', 'w');
fwrite(fid, wavbinary, 'uint8');
fclose(fid);
fid = fopen('wav.bin', 'r');
A = fread(fid,'*uint8');
wavdata = uint16(reshape(bin2dec( char(reshape(A, 8, []).' + '0') ), [], 2));
SampleRate = 22100;
audiowrite('FileNameGoesHere.wav', wavdata, SampleRate)
1 Commento
Lukas Kuhl
il 6 Lug 2019
Alaa Hesham
il 16 Dic 2019
If final_audio_output is output represented as vector of zeros and ones , the original audio file was matlab " handel.wav".
Final_audio_output is after I have done processing on it (modulation + adding noise , then demodulation)
scaled_audio=reshape(final_audio_output,[],8); % Orignal audio file was of size 292452*8 " handel.wav file"
t_scaled_audio= typecast( uint8(bin2dec(char(scaled_audio + '0 ') )), 'int32' ) % this is the format that should be entered
% to audiowrite
fs=8192
audiowrite('r_handel.wav',t_scaled_audio,fs)
Categorie
Scopri di più su Signal Import and Export in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!