How to read MP3 in MATLAB - matlab

I am currently working on watermarking audio files in Matlab for a mathematics research project. So far I have been able to read wav files using wavread in Matlab. However, because wav files are very large, the resulting data is also large. Therefore, in order to simplify this I would like to know how I would be able to read an mp3 file in Matlab. So far I have only tried to read an mp3 by using dsp.AudioFileReader. However, the resulting data only contains 0's and a few other numbers, which is clearly not the correct data. Would someone please be able to help me? Thank you so much!

you can use this code:
hfr = dsp.AudioFileReader('yourfile.mp3');
hplay = dsp.AudioPlayer('SampleRate',sample_rate_here);
 while ~isDone(hfr)
         audio = step(hfr);
         step(hplay, audio);
end

Related

Convert .mid to .wav using MATLAB

I've been using this library (http://kenschutte.com/midi) to work with midi files and the functions on here have been very helpful. However, the midi2audio() method only produces garbled .wav files no matter what midi I put in (although the notes are recognizable and the correct midi is being played). Has anyone else used this function library and run into this same problem and if so, how could I fix this? Or is there another function I can use online somewhere that does the same thing?
Below is the code used to generate the .wav file (copied and pasted from the link above)
[y,Fs] = midi2audio(midi);
% save to file:
% (normalize so as not clipped in writing to wav)
y = .95.*y./max(abs(y));
wavwrite(y, Fs, 'out.wav');
It appears that midi2audio only include very rudimentary sound synthesis, with frequency modulated synthesis as default. If you change to simple sine wave synthesis maybe it will sound better?
[y,Fs] = midi2audio(midi, 'sine')
If that still doesn't cut it you'd probably want to use more sophisticated software instruments.
The simplest cross-platform method for this is probably FluidSynth (also available through various repositories like MacPorts, Homebrew, apt-get, GitHub…)
FluidSynth uses sample based sound synthesis to translate the MIDI instructions into audio, and a sample bank in the SoundFont2 format is required for it to work. One such can be found here.
Having sorted that out, all you have to do to make a WAVE file out of your MIDI file is to type this into your terminal/console:
fluidsynth -F out.wav path-to-fm2-file in.mid

Read and represent mp3 files using memmapfile in matlab

I have to analyze bio acoustic audiofiles using matlab. Eventually I want to be able to find anomalies in the audio. That's the reason I need to find a way to represent the audio in a way I can extract and compare features. I'm dealing with mp3 files up to 150 mb. These files are too large for matlab to read in to it's memory. Therefore I want to use the memmapfile() function. I used the following code and a small mp3 file to find out how it actually works.
[testR, ~] = audioread('test.mp3');
testM = memmapfile('test.mp3');
disp(testM.Data);
disp(testR);
The actual values of the testM.Data and testR are different. Audioread() returns a 7483391 x 2 matrix and memmapfile() a 4113874 x 1 matrix.
I'm not really sure how memmapfile() works, I expected this to be equal to each other. Is there a way to read mp3 files in the same format audioread() does using memmapfile()? And what does memmapfile actually return in case of an audio file? Maybe it's also usable in the vector format in the case of anomaly detection?
Thanks in advance!
NOTE: The original files were in wav IMA ADPCM format with sizes from 1.5 up to 2.5 gb. Since Matlab can't deal with that format and the size of the files I converted them to 8bit mp3 files.
I think that the problem is mammapfile by default read data in uint8 format, while audioread function read data in another way.
How you can see here you can specify the format of data when you read it with memmapfile, so try to "play" with different values. From the documentation I read that you can read data in double format, so try to modify the memmapfile data format and audioread data format.
Last thing, memmapfile always organize the data in matrix like "somenumbers x 1", so if you want the original one you need to use something like reshape.
Anyway if you work with big data I suggest you to try with something different instead memmapfile, because it is very very slow

Conversion from DICOM to YUV file

I have a series of DICOM files that I loaded on matlab. I would need to create a YUV file from the DICOM slices.Does anybody know how to do so?
Regards,
Note sure if this answers your question, but there is some code for this on the MATLAB file exchange:
http://www.mathworks.com/matlabcentral/fileexchange/11264-matlab-movie-to-yuv-file
I know this is for movies, but I think you will be able to use this code (maybe adapted) to run it for each slice separately, if that's what you want.

Matlab Audio Format- How to process?

I have downloaded a EEG recording of a person in Matlab-Audio Format. I have no idea of how to use it in Matlab for further processing. Is it possible to generate signals in Matlab? If so, is there any code to generate EEG signal?
Any help would be highly appreciated. Thanks in advance!
Some versions of Winamp and Microsoft Access set the description for the .mat file extension to "MATLAB Audio Format", although I have no idea what this means. It's unclear to me if such a unique file type really exists; I'd love to know more if there is actually some special format of which I am not aware. More likely, the file you downloaded is simply a standard MATLAB .mat file containing the EEG data in one or more variables. You can read the variables in MATLAB with the load command. To see the variable names contained in the MAT file prior to loading it, use whos with the -file switch.

How can I get into a wav file to change the sample rate?

I have a wav file pulled up in MATLAB, and I can see it's sample rate. All I need to do is change this 1 number. Everything else in the file will remain uncahnged. (The resulting sound would play at a different speed but would have an identical array of sample data.)
The reason I need to do this is because MATLAB seems to freak out when I tell it to open something sampled at anything other than 8k. All I need MATLAB for is to edit the file, so the sample rate really doesn't matter at all, since I'll be putting it back into a wav file when I'm done. So I either need to be able to change the value in the wav file that stores the sample rate, or to get MATLAB to change the sample rate it prefers from 8k to the sample rate that my files were recorded at.
if you just want to change the sampling frequency, here is the code, but it would distort the original wav file. If you decrease the sampling frequency, then the beat and music would be very slow.
Code:
[y, fs, nbits]=wavread('stego_lab');
fs2=11025;
wavwrite(y,fs2,nbits,'stego2_lab.wav');
sound(y,fs2,nbits)
you can hear it but the samples will remain the same.
Hope it helps.
There is the SOX tool, which should help you in that respect, and it comes on almost any platform - http://sox.sourceforge.net
There is also libsndrate, libsamplerate, libsndfile and others, that might have executables too.
Try this solution
[x,fs] = wavread('infile.wav');
<br>[p,q] = rat(16000/fs) % to convert to 16k sample rate</br>
<br>y = resample(x,p,q); % signal package require
wavwrite(x,16000,'outfile.wav');