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