How to Convert .wav file into binary?

load handel.mat
filename = 'handel.wav';
audiowrite(filename,y,Fs);
clear y Fs
[y,Fs] = audioread('handel.wav');
sound(y,Fs)
I want to use the following code to convert the .wav audio file into binary. Which MATLAB command should be used for converting .wav file in to binary?
TIA.

4 Commenti

Jan
Jan il 7 Mar 2018
What exactly is "binary" in your case?
Binary stream (zeros and ones).
Jan
Jan il 7 Mar 2018
Modificato: Jan il 7 Mar 2018
@Asra: Please try to explain the problem clearly. A binary stream of what? The signal or the contents of the file? If the data are stored in floating point format, do you want to convert the data to int8/16/32 at first or is is sufficient to interpret the binary representation of the IEEE754 doubles? Do you want a char vector or UINT8?
The less the readers have to guess, the easier and more likely matching is the answer.
Jan
Jan il 7 Mar 2018
Modificato: Jan il 7 Mar 2018
[MOVED from section for answers]
I have to transmit sound file (.wav file) through LED's. For that I have to convert the .wav file in binary digits (0's & 1's) to turn LED's ON and OFF. I need the contents of the file to be converted into binary digits.
I hope it is more clear to you now.
[Please post comments in the section for comments]

Accedi per commentare.

 Risposta accettata

Jan
Jan il 7 Mar 2018
Modificato: Jan il 7 Mar 2018
If you simply want to get a bit-stream representation, it does not matter in any way, if the data are a wav file or a jpeg. A WAV file contains additional header information compared to the pure signal. Getting a bit-stream from it:
fid = fopen(FileName, 'r');
data = fread(fid, [1, Inf], 'uint8');
fclose(fid);
bit = uint8(rem(floor((1 ./ [128, 64, 32, 16, 8, 4, 2, 1].') * data), 2));
bit = bit(:);
Now you have a bit stream of 1s and 0s stored in an UINT8 vector. This contains the complete data of the file and does not care in any way about the contents or format.

10 Commenti

bit = uint8(rem(floor((1 ./ [128, 64, 32, 16, 8, 4, 2, 1].') * data), 2));
bit = bit(:);
Can you please explain these two lines?
Jan
Jan il 7 Mar 2018
Modificato: Jan il 7 Mar 2018
Imagine that data is [1, 3, 6, 8]. Now it is multiplied by the vector:
1 ./ [128, 64, 32, 16, 8, 4, 2, 1].'
This means a division by powers of 2. If you round this result towards zero by floor, the remainder to 2 is the binary representation of the input element.
Try this for 163:
163 ./ [128,64,32,16,8,4,2,1]
[1.27, 2.55, 5.09, 10.19, 20.38, 40.75, 81.5, 163.0]
Now floor it:
[1, 2, 5, 10, 20, 40, 81, 163]
and get the remainder to 2:
[1, 0, 1, 0, 0, 0, 1, 1]
This means that 163 equal 1*128 + 1*32 + 1*2 + 1*1.
bit = bit(:)
reshapes the matrix bit to a column vector.
The shown method is more direct than the nicer:
bit = dec2bin(data) - '0';
dec2bin creates a char vector of '0' and '1' by a method equivalent to my suggestion. Converting it back to numerical data is an indirection.
How would I write this back to become an audio file
Jan
Jan il 21 Apr 2021
@Shannon Marie Falter: It depends on what you call "an audio file". Maybe with fwrite or with audiowrite.
Well I do some work on the file after reading it then result with another binary sequence. I need to right this into an audio file of .flac or .wav, but I believe I need to change it back to numbers from binary first and don't know how.
I am trying to compare the original audio file with this one so I need to hear it.
Jan
Jan il 22 Apr 2021
For hearing it you can play the original and modified arrays directly in Matlab using audioplay.
I want to do the same with a whole audio datset..how do i do that? I dont want to do it seperately for each audio file
Jan
Jan il 17 Mag 2021
What is "a whole audio datset"?
fid = fopen(FileName, 'r');
what is 'r'?
Whenever you have a question to a specific command, start with reading the documentation:
doc fopen
If you do not have a local Matlab installation, serach online for "Matlab fopen" to find: https://www.mathworks.com/help/matlab/ref/fopen.html#btrnibn-1-permission . Here you see:
'r' Open file for reading.

Accedi per commentare.

Più risposte (1)

Richiesto:

il 7 Mar 2018

Commentato:

Jan
il 31 Mag 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by