MATLAB: Using WavWrite to Create a Single Audio File

  • Context: MATLAB 
  • Thread starter Thread starter mushiman
  • Start date Start date
  • Tags Tags
    Audio File Matlab
Click For Summary

Discussion Overview

The discussion revolves around creating a single audio file in MATLAB using multiple generated audio samples, specifically focusing on the use of the 'wavwrite' function. Participants explore methods for combining several tones into one 'wav' file, addressing both the technical implementation and code efficiency.

Discussion Character

  • Technical explanation
  • Homework-related
  • Conceptual clarification

Main Points Raised

  • One participant describes their struggle with generating multiple audio samples in MATLAB and the need to combine them into a single 'wav' file.
  • Another participant suggests using a loop to concatenate the generated audio samples into a single matrix before writing it to a file.
  • A follow-up response indicates that the suggested solution worked effectively for the original poster.
  • A subsequent question is raised about optimizing the code by using a matrix of frequencies to avoid repetitive code for each tone generation.

Areas of Agreement / Disagreement

Participants generally agree on the approach to combine audio samples into a single file, but there is ongoing discussion about optimizing the code structure, indicating that multiple views on implementation exist.

Contextual Notes

Limitations include the lack of specific details on the function used to generate the audio samples and the absence of clarity on how to efficiently manage the frequency values in the code.

Who May Find This Useful

Individuals learning MATLAB, particularly those interested in audio processing and file management within the software.

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 5 ·
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 20 ·
Replies
20
Views
4K