MATLAB *.pcm to *.wav converting - matlab

How can I convert *.pcm audio file to *.wav audio File in MATLAB-Code?
I just need to insert a header, but how it is work?
Thank you very much!

Since you didn't specify it, I'm assuming you are using Matlab 2018b, so I will point you to the most recent documentation about audioread:
As you can see, PCM is not on the supported format list.
You should try to look if you can parameter your AudioRecorder to record your audio to another format within the ones in the supported list: .wav, .ogg, .flac, .au, .aif, .aifc, mp3, .mp4...
An alternative option, without using audioread, would be to import pcm data like any other data file, then convert it to 16 bit wav. I assume sample rate is 44100 Hz.
fid = fopen('audioFile.pcm'); % Open raw pcm file
audio = int16(fread(fid, Inf, 'int16')); % Convert data into 16 bit
fclose(fid); % Close pcm file
audiowrite('audioFile.wav', audio, 44100,'BitsPerSample', 16); % Write wav

Related

MATLAB: Use of vision.VideoFileWriter and vision.VideoFileReader

I'm trying to convert a .avi file with audio to a .mp4 file. I wrote this script 'avi2mp4.m' using the Computer Vision System Toolbox v7.2 with the MATLAB R2016b.
vfr = vision.VideoFileReader('Cris Drift vs Patrick.avi', 'AudioOutputPort',true);
vfw = vision.VideoFileWriter('Cris Drift vs Patrick.mp4', 'FileFormat','MPEG4', 'AudioInputPort',true, ...
'FrameRate',vfr.info.VideoFrameRate, 'Quality',90);
while ~isDone(vfr)
[frame, audio] = vfr(); % [frame, audio] = step(vfr);
vfw(frame, audio); % step(vfw, frame, audio);
end
release(vfr);
release(vfw);
but i get this error:
Error using vision.VideoFileWriter/parenReference
Too many input arguments; expected 1 (in addition to the object handle), got 2.
Error in avi2mp4 (line 16)
vfw(frame, audio);
I don't know why? I have to pass the audio data as an argument to write it with the video data. It's the same syntax as described in the MATLAB Documentation
Documentation for Video File Writer Object
Documentation for Video File Reader Object
With vision.VideoFileWriter you can write both audio and video only when the format is AVI or WMV. If you received a warning about AudioInputPort property not relevant when you set that property that means audio is not supported in that configuration.

Extracting audio from video in matlab. The audio is de-accelerated

The input video length is 1min 56sec and Ouput audio length comes out to be 2 min 47 sec
file1='vipmen1.wav'; %o/p file name
hmfr=video.MultimediaFileReader(file_fullpath,'AudioOutputPort',true,'VideoOutputPort',false);
hmfw = video.MultimediaFileWriter(file1,'AudioInputPort',true,'FileFormat','WAV');
while ~isDone(hmfr)
audioFrame = step(hmfr);
step(hmfw,audioFrame);
end
close(hmfw);
close(hmfr);
You have to use the same sample rate for your output. Read the sample rate from the input and use this rate to write the output.

Segmented speech data, save it as a .wav file and play it in Matlab

I have speech data, which I segment into some parts. I am trying to create a .wav file for each segmented data and play this .wav file.
For example, suppose the speech data is an 1x1000 array called data. I segment this data into 4 parts using the indices of seg_data.
seg_data
1 250
251 500
501 750
751 1000
Code:
for i=1:size(seg_data(:,1))
w(i)=data(seg_data(i,1): seg_data(i,2));
wavwrite(w(i),'file_%d \n ',i);
end
First thing I need to create one folder in which the .wav file that is created in every iteration is stored. Then, I can read the stored file one by one and can play it.
If you don't know the sampling frequency Fs, you have to use the signature wavwrite(y,filename) as presented in the reference site.
for i=1:size(seg_data(:,1))
w(i,:)=data(seg_data(i,1): seg_data(i,2));
wavwrite(w(i,:),['file_',num2str(i)]);
end
BTW, the documentation suggests to use audiowrite instead.

How can I convert a sound file to .mif using MATLAB?

I want to have a mif file from the sound signal of a for example mp3 file. How can I do it using MATLAB?
Thanks,
BooMZ!
As result, I couldn't find any solution to convert a MP3 file to mif. But I could convert wav file to mif. It's the instructions:
First you must drag and drop your wav file to MatLab. MatLab will make a array from wav file. Finally, you must write the data array to a file with mif format. (you can do it by fprintf)

Is there a function in MATLAB that converts a .raw file into a matrix?

I want to import a .raw file into MATLAB as a matrix (frames x spatial x spectral). Is there a built-in function to do this?
If you're referring to a raw image file from a camera, I would check out the submission RAW Camera File Reader from Bryan White on the MathWorks File Exchange.
You can also read the file directly. But you'll need to convert it to DNG first. See this:
http://blogs.mathworks.com/steve/2011/03/08/tips-for-reading-a-camera-raw-file-into-matlab/