MATLAB MATLAB: Using WavWrite to Create a Single Audio File

AI Thread Summary
A MATLAB user is experiencing difficulties in generating and saving audio samples as a single 'wav' file. Initially, they create individual tones using a sine function and the sound() function, but they struggle to combine these tones into one file. The user is advised to accumulate the generated audio samples into a single matrix using a loop, which allows for sequential playback and saving as a single 'wav' file. The proposed solution involves initializing an empty matrix and appending each generated tone within a loop. This method successfully resolves the user's issue. They later inquire about optimizing their code by defining multiple frequencies in a single matrix to reduce the overall code length, suggesting a desire for a more efficient implementation.
mushiman
Messages
10
Reaction score
0
I am a MATLAB newbie, and apparently I am having a terrible day because everything that I do in MATLAB has been resulting in an error and/or undesired behavior.

I am trying to write out a 'wav' file using a number of MATLAB created audio samples (simply by generating them using a sin function, and outputting them using sound();). The problem is that I need to create several sequentially, and create a single file that will have each in order.

As it stands, I am creating each tone individually, and therefore I cannot write them into a single 'wav' file with my low knowledge of MATLAB.

Right now, I am doing something like this:
Code:
f0 = 220;
fs = 16000;
x = (insert my function here);
sound(x,fs);
wavwrite(x,fs,bits,'audio.wav');

Of course, I have to create a new batch of code like this for every new value of f0 that I need (and there are quite a few), but for my purposes, I suppose that this can work.

I need to know how to write the value(s) of 'x' into a matrix or something to that effect so that I can have the final result of all of my audio clips combined into a single 'wav' file that plays them sequentially. Any help would be greatly appreciated -- I can provide more detail on this issue if necessary.
 
Physics news on Phys.org
You could do this...

Code:
xbuff = [];
for k = 1:(the number of tones)
    x     = (insert your function here);
    xbuff = [xbuff; x(:)];
end

sound(xbuff,fs);
wavwrite(xbuff,fs,bits,'audio.wav');
 
I will give that a try and post back. Thank you!

Edit: This worked like a charm. Thanks again.
 
Last edited:
Quick follow-up question: Is there a more compact way that I can implement this function into my code? Right now I'm starting a new loop (k = 1:1) and redefining the frequency of the sinusoid for each tone. Is there a way to implement all of the frequencies into one large matrix and have it loop (ie, f = [220 440 880 1760 etc]? Right now, I'm looking at about 200 lines of code or so.

I'm sure there is an easy way to do it, but I'm not seeing it right off the top of my head at the moment... I know it's a pretty easy question, but I'm rather tired right now.
 
Last edited:

Similar threads

Replies
1
Views
2K
Replies
5
Views
2K
Replies
2
Views
2K
Replies
5
Views
2K
Back
Top