Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB: Using WavWrite to Create a Single Audio File

  1. Oct 4, 2007 #1
    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 (Text):
    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.
     
  2. jcsd
  3. Oct 4, 2007 #2
    You could do this...

    Code (Text):
    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');
     
  4. Oct 4, 2007 #3
    I will give that a try and post back. Thank you!

    Edit: This worked like a charm. Thanks again.
     
    Last edited: Oct 4, 2007
  5. Oct 4, 2007 #4
    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: Oct 4, 2007
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?