WAV into binary bits
18 views (last 30 days)
Show older comments
How to convert a wav file data into binary (0,1) format, so that it can again be converted from binary form to wav?
0 Comments
Accepted Answer
Walter Roberson
on 18 Mar 2013
wavdata = wavread('FileName.wav');
wavbinary = dec2bin( typecast( single(wavdata(:)), 'uint8'), 8 ) - '0';
orig_size = size(wavdata);
Then the reverse:
wavdata = reshape( typecast( bin2dec( uint8(wavbinary + '0') ), 'single' ), orig_size );
6 Comments
Nelson Rodriguez Hernandez
on 8 Jul 2016
Edited: Nelson Rodriguez Hernandez
on 8 Jul 2016
But if I have a binary array of only "Zeros and Ones"(0,1), how can I create sound from binary in .wav or other extension? Do I have to create my own samples using audiowrite, bin2dec or what? In other words how can I sampling from binary?
Walter Roberson
on 8 Jul 2016
Assuming your binary array of 0 and 1 is stored in wavbinary then
data_class_to_use = 'int32'; %or as appropriate
SampleRate = 22100; %set as appropriate
wavdata = reshape( typecast( uint8(bin2dec( char(wavbinary + '0') )), data_class_to_use ), orig_size );
audiowrite('FileNameGoesHere.wav', wavdata, SampleRate)
The data_class_to_use will depend upon kind of data you intend your binary array to represent. The sample code I gave here assumes that your binary array uses groups of 32 bits per sample, and that the 32 bits represent a signed 32 bit integer. For the question that was originally posted, it was 'float' that was appropriate to use, because the binary array represented 32 bit single precision numbers.
More Answers (2)
Jan
on 19 Mar 2013
What exactly are "wav file data"? Are you talking about doubles or integer types?
What is the prupose of converting the data to a binary format, when you consider, that all data are store in a binary format inside the computer. What do you want to achieve?
3 Comments
Kushant
on 2 Apr 2014
i also have the same problem...
i want to use generated c code on dsp, but matlab coder do not support wavread function. i am planning to use fopen and fread but for that i need binary files instead of .wav files.
please tell me how to do it?
and how it can be done arithmatically?
Walter Roberson
on 2 Apr 2014
Outside of the dsp, wavread() the .wav files, and fwrite() them to a binary file. In the dsp read the binary file.
Kyaw Yegaung
on 24 Sep 2018
I get the problem converting wav file to binary with wave file read command wavread. But i use another command audioread. Authoug it was work i got trouble in another command like dec2bin. It said that deceimal be less than 2 power 52. The point is what actually the data type of wav or other audio source file and how can i convert to decimal before converting binary data. If anyone could help me please contact my mail 'oooooyaungoooo@gmail.com'. Thank you.
0 Comments
See Also
Categories
Find more on Audio I/O and Waveform Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!