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.
root / 2-sndfile-read-and-play.py
History | View | Annotate | Download (1.47 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 | ############# PLAYING AN AUDIO FILE #############
|
||
| 44 | #################################################
|
||
| 45 | |||
| 46 | 6:fd0f9d0615b2 | adamstark | # play the audio file data in 'samples' at the sampling frequency 'fs'
|
| 47 | play(samples,fs) |
||
| 48 | 4:2b996e1d64da | adamstark | |
| 49 | |||
| 50 | #################################################
|
||
| 51 | #################################################
|
||
| 52 | ################################################# |