Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: How to cut sound wave files

Subject: How to cut sound wave files

From: Damon Ng

Date: 7 Jul, 2008 19:27:02

Message: 1 of 7

Hey all,

I'm now learning to use MATLAB to modify and analyze sounds
for experiments. I'd like to know how to edit sound waves
and then save them in a separate folder.

I have 500 wave files, each varies at different time
lengths. For each sound, I just want the first 500ms
segment, and then save them for further analysis in a
different folder (it is okay to discard the leftover).
Except extracting the 1st 500ms segment for each sound,
there is no need to change any other acoustic parameters.

How could I write the m-file, so that I can process them all
at once (e.g. in a loop format).

The signal GUI toolbox only allows me to do one at a time
and it is a very tedious process.

Thanks for reading my inquiry


Subject: How to cut sound wave files

From: Lothar Schmidt

Date: 7 Jul, 2008 19:36:34

Message: 2 of 7

Damon Ng wrote:

> Hey all,
>
> I'm now learning to use MATLAB to modify and analyze sounds
> for experiments. I'd like to know how to edit sound waves
> and then save them in a separate folder.
>
> I have 500 wave files, each varies at different time
> lengths. For each sound, I just want the first 500ms
> segment, and then save them for further analysis in a
> different folder (it is okay to discard the leftover).
> Except extracting the 1st 500ms segment for each sound,
> there is no need to change any other acoustic parameters.
>
> How could I write the m-file, so that I can process them all
> at once (e.g. in a loop format).
>
> The signal GUI toolbox only allows me to do one at a time
> and it is a very tedious process.
>
> Thanks for reading my inquiry

help wavread
help wavwrite

you won't need much more.

Lothar

Subject: How to cut sound wave files

From: Steve Amphlett

Date: 7 Jul, 2008 21:24:01

Message: 3 of 7

Lothar Schmidt <vapooroop@gmx.net> wrote in message <g4tr82
$8vh$1@online.de>...
> Damon Ng wrote:
>
> > Hey all,
> >
> > I'm now learning to use MATLAB to modify and analyze
sounds
> > for experiments. I'd like to know how to edit sound
waves
> > and then save them in a separate folder.
> >
> > I have 500 wave files, each varies at different time
> > lengths. For each sound, I just want the first 500ms
> > segment, and then save them for further analysis in a
> > different folder (it is okay to discard the leftover).
> > Except extracting the 1st 500ms segment for each sound,
> > there is no need to change any other acoustic
parameters.
> >
> > How could I write the m-file, so that I can process
them all
> > at once (e.g. in a loop format).
> >
> > The signal GUI toolbox only allows me to do one at a
time
> > and it is a very tedious process.
> >
> > Thanks for reading my inquiry
>
> help wavread
> help wavwrite
>
> you won't need much more.

Except perhaps a detailed read of ML's <dir> function. It
can return the list of files you want to loop through.

Subject: How to cut sound wave files

From: Damon Ng

Date: 8 Jul, 2008 03:01:03

Message: 4 of 7

Thanks for both of you.

Subject: How to cut sound wave files

From: rodney.thomson@gmail.com

Date: 8 Jul, 2008 05:50:35

Message: 5 of 7

On Jul 8, 3:36=A0am, Lothar Schmidt <vapoor...@gmx.net> wrote:
> Damon Ng wrote:
> > Hey all,
>
> > I'm now learning to use MATLAB to modify and analyze sounds
> > for experiments. I'd like to know how to edit sound waves
> > and then save them in a separate folder.
>
> > I have 500 wave files, each varies at different time
> > lengths. For each sound, I just want the first 500ms
> > segment, and then save them for further analysis in a
> > different folder (it is okay to discard the leftover).
> > Except extracting the 1st 500ms segment for each sound,
> > there is no need to change any other acoustic parameters.
>
> > How could I write the m-file, so that I can process them all
> > at once (e.g. in a loop format).
>
> > The signal GUI toolbox only allows me to do one at a time
> > and it is a very tedious process.
>
> > Thanks for reading my inquiry
>
> help wavread
> help wavwrite
>
> you won't need much more.
>
> Lothar

As Lothar wrote, wavread to read the data in, wavwrite to write it
out:

directory =3D 'PUT HERE WHERE YOUR WAVE FILES ARE';

contents =3D dir(directory);

for i =3D 1:length(contents)
  in_file =3D contents(i).name;
  if (strfind(in_file, '.wav'))
    fprintf(1, 'Trimming %s\n', in_file);

    % read contents of wav file
    [in_data Fs nbits] =3D wavread(in_file);

    % work out how many samples are in 500 ms
    num_samples =3D round(0.5*Fs);

    % write this out to file
    wavwrite(in_data(1:num_samples), Fs, nbits,
sprintf('%s_TRIMMED.wav', in_file);
  end
end

Subject: How to cut sound wave files

From: rodney.thomson@gmail.com

Date: 8 Jul, 2008 07:35:25

Message: 6 of 7

On Jul 8, 3:36=A0am, Lothar Schmidt <vapoor...@gmx.net> wrote:
> Damon Ng wrote:
> > Hey all,
>
> > I'm now learning to use MATLAB to modify and analyze sounds
> > for experiments. I'd like to know how to edit sound waves
> > and then save them in a separate folder.
>
> > I have 500 wave files, each varies at different time
> > lengths. For each sound, I just want the first 500ms
> > segment, and then save them for further analysis in a
> > different folder (it is okay to discard the leftover).
> > Except extracting the 1st 500ms segment for each sound,
> > there is no need to change any other acoustic parameters.
>
> > How could I write the m-file, so that I can process them all
> > at once (e.g. in a loop format).
>
> > The signal GUI toolbox only allows me to do one at a time
> > and it is a very tedious process.
>
> > Thanks for reading my inquiry
>
> help wavread
> help wavwrite
>
> you won't need much more.
>
> Lothar

Hmmm this might be a double post, apologies if it is (content is
slightly different anyway):

in_dir =3D 'DIRECTORY CONTAINING WAV FILES';

contents =3D dir(in_dir);

for i =3D 1:length(contents)
  in_file =3D contents(i).name;

    if (strfind(in_file, '.wav))

      fprintf(1, Trimming '%s\n', in_file);

      % Read data
      [y Fs n_bits] =3D wavread(fullfile(in_dir, in_file));

      % Work out how many samples in 0.5 seconds
      n_samples =3D round(0.5*Fs);

      % Write back out to dile, with TRIMMED_ pre-pended
      wavwrite(y(1:n_samples), Fs, n_bits, fullfile(in_dir,
sprintf('TRIMMED_%s', in_file)));

    end
end

--
http://iheartmatlab.blogspot.com

Subject: How to cut sound wave files

From: Damon Ng

Date: 8 Jul, 2008 16:51:02

Message: 7 of 7

Thanks again

I initially get confused with dir and string parts.

Now I get the ideas.


Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics