Revision 1:003b86796d1b audio_io.py
| audio_io.py | ||
|---|---|---|
| 40 | 40 |
|
| 41 | 41 |
# play the audio file data in 'samples' at the sampling frequency 'fs' |
| 42 | 42 |
audiolab.play(samples,fs) |
| 43 |
|
|
| 44 |
################# |
|
| 45 |
## Choosing only the first half second of audio |
|
| 46 |
################# |
|
| 47 |
|
|
| 48 |
# the new number of samples is the |
|
| 49 |
# (duration times sampling frequency) |
|
| 50 |
new_nsamples = 0.5 * fs |
|
| 51 |
|
|
| 52 |
# creating the new samples array |
|
| 53 |
new_samples = samples[0:new_nsamples] |
|
| 54 |
|
|
| 55 |
################# |
|
| 56 |
## Writing the shorter audio to a new file |
|
| 57 |
################# |
|
| 58 |
|
|
| 59 |
# create a name for the new file |
|
| 60 |
new_filename = 'output_file.wav' |
|
| 61 |
|
|
| 62 |
# Create a Sndfile instance for writing a mono wav file @ 44100 Hz |
|
| 63 |
format = audiolab.Format('wav')
|
|
| 64 |
f = audiolab.Sndfile(new_filename, 'w', format, 1, 44100) |
|
| 65 |
|
|
| 66 |
# Write out the shorter audio file |
|
| 67 |
f.write_frames(new_samples) |
|
| 68 |
|
|
| 69 |
# close the audio file |
|
| 70 |
f.close() |
|
Also available in: Unified diff