Mercurial > hg > swc-python-audio
changeset 1:003b86796d1b
Creates/saves a new wav file that contains only the first half second of audio of the original file.
| author | luisf <luis.figueira@eecs.qmul.ac.uk> |
|---|---|
| date | Mon, 11 Feb 2013 16:00:03 +0000 |
| parents | ea12496723d2 |
| children | 9885d7b7aca0 |
| files | audio_io.py |
| diffstat | 1 files changed, 28 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/audio_io.py Mon Feb 11 15:41:00 2013 +0000 +++ b/audio_io.py Mon Feb 11 16:00:03 2013 +0000 @@ -40,3 +40,31 @@ # play the audio file data in 'samples' at the sampling frequency 'fs' audiolab.play(samples,fs) + +################# +## Choosing only the first half second of audio +################# + +# the new number of samples is the +# (duration times sampling frequency) +new_nsamples = 0.5 * fs + +# creating the new samples array +new_samples = samples[0:new_nsamples] + +################# +## Writing the shorter audio to a new file +################# + +# create a name for the new file +new_filename = 'output_file.wav' + +# Create a Sndfile instance for writing a mono wav file @ 44100 Hz +format = audiolab.Format('wav') +f = audiolab.Sndfile(new_filename, 'w', format, 1, 44100) + +# Write out the shorter audio file +f.write_frames(new_samples) + +# close the audio file +f.close()
