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 / sndfile-write.py @ 1:99434d86405a

History | View | Annotate | Download (1.77 KB)

1
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
nsamples = f.nframes
32

    
33

    
34
#################################################
35
######## READ AUDIO SAMPLES FROM THE FILE #######
36
#################################################
37

    
38
# we can read audio samples using the read_frame method
39
data = f.read_frames(nsamples)
40

    
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
output_data = data
51

    
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
f.write_frames(output_data[:fs*3])
58

    
59
# close the audio file
60
f.close()
61

    
62
#################################################
63
#################################################
64
#################################################