To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

The primary repository for this project is hosted at https://github.com/Codasign/york-software-bootcamp-audio-day.git .
This repository is a read-only copy which is updated automatically every hour.

Statistics Download as Zip
| Branch: | Revision:

root / 3-sndfile-write.py

History | View | Annotate | Download (1.78 KB)

1 4:2b996e1d64da adamstark
from scikits.audiolab import Sndfile
2
from scikits.audiolab import Format
3
from scikits.audiolab import play
4
5
#################################################
6
######## CREATING A SOUND FILE INSTANCE #########
7
#################################################
8
9
# create Sndfile instance with our example audio file
10
f = Sndfile('viola.wav', 'r')
11
12
13
#################################################
14
######## EXTRACTING AUDIO FILE META-DATA ########
15
#################################################
16
17
# extract and print sample rate
18
fs = f.samplerate
19
print "sample rate: ",fs
20
21
# extract and print the number of channels
22
nc = f.channels
23
print "number of channels: ",nc
24
25
# extract and print the encoding format
26
enc = f.encoding
27
print "encoding format: ",enc
28
29
# extract the number of frames - single samples for
30
# mono and pairs of samples for stereo
31 6:fd0f9d0615b2 adamstark
num_samples = f.nframes
32 4:2b996e1d64da adamstark
33
34
#################################################
35
######## READ AUDIO SAMPLES FROM THE FILE #######
36
#################################################
37
38
# we can read audio samples using the read_frame method
39 6:fd0f9d0615b2 adamstark
samples = f.read_frames(num_samples)
40 4:2b996e1d64da adamstark
41
42
#################################################
43
########## WRITING TO A NEW AUDIO FILE ##########
44
#################################################
45
46
# create a name for the new file
47
new_filename = 'output_file.wav'
48
49
# create the output audio data, in this case a simple copy
50 6:fd0f9d0615b2 adamstark
output_samples = samples
51 4:2b996e1d64da adamstark
52
# Create a Sndfile instance for writing wav files @ 44100 Hz
53
format = Format('wav')
54
f = Sndfile(new_filename, 'w', format, 1, 44100)
55
56
# Write out the first 3 seconds worth of samples (fs*3)
57 6:fd0f9d0615b2 adamstark
f.write_frames(output_samples)
58 4:2b996e1d64da adamstark
59
# close the audio file
60
f.close()
61
62
#################################################
63
#################################################
64
#################################################