Revision 0:ea12496723d2
| audio_io.py | ||
|---|---|---|
| 1 |
# imports audiolab |
|
| 1 | 2 |
import scikits.audiolab as audiolab |
| 2 |
import pylab as plt |
|
| 3 |
import numpy as np |
|
| 4 | 3 |
|
| 5 | 4 |
# create Sndfile instance with our example audio file |
| 6 | 5 |
f = audiolab.Sndfile('viola.wav', 'r')
|
| ... | ... | |
| 41 | 40 |
|
| 42 | 41 |
# play the audio file data in 'samples' at the sampling frequency 'fs' |
| 43 | 42 |
audiolab.play(samples,fs) |
| 44 |
|
|
| 45 |
################# |
|
| 46 |
## Choosing only the first half second of audio |
|
| 47 |
################# |
|
| 48 |
|
|
| 49 |
# the new number of samples is the |
|
| 50 |
# (duration times sampling frequency) |
|
| 51 |
new_nsamples = 0.5 * fs |
|
| 52 |
|
|
| 53 |
# creating the new samples array |
|
| 54 |
new_samples = samples[0:new_nsamples] |
|
| 55 |
|
|
| 56 |
################# |
|
| 57 |
## Plotting the new signal using pylab/matplotlib |
|
| 58 |
################# |
|
| 59 |
|
|
| 60 |
# the x axis now shows the time in seconds instead of the sample number |
|
| 61 |
## using the Numpy function linspace in order to create the time array |
|
| 62 |
x_axis = np.linspace(0, 0.5, new_samples.size) |
|
| 63 |
plt.plot(x_axis, new_samples) |
|
| 64 |
plt.show() |
|
| 65 |
|
|
| 66 |
################# |
|
| 67 |
## Writing the shorter audio to a new file |
|
| 68 |
################# |
|
| 69 |
|
|
| 70 |
# create a name for the new file |
|
| 71 |
new_filename = 'output_file.wav' |
|
| 72 |
|
|
| 73 |
# Create a Sndfile instance for writing a mono wav file @ 44100 Hz |
|
| 74 |
format = audiolab.Format('wav')
|
|
| 75 |
f = audiolab.Sndfile(new_filename, 'w', format, 1, 44100) |
|
| 76 |
|
|
| 77 |
# Write out the shorter audio file |
|
| 78 |
f.write_frames(new_samples) |
|
| 79 |
|
|
| 80 |
# close the audio file |
|
| 81 |
f.close() |
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
Also available in: Unified diff