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 / 3a-sndfile-convert-wav-to-aiff-in-folder.py
History | View | Annotate | Download (1.18 KB)
| 1 | 4:2b996e1d64da | adamstark | import os |
|---|---|---|---|
| 2 | from scikits.audiolab import Sndfile |
||
| 3 | from scikits.audiolab import Format |
||
| 4 | |||
| 5 | # get the current working directory
|
||
| 6 | path = os.getcwd() |
||
| 7 | |||
| 8 | # get all file names in the current directory
|
||
| 9 | files = os.listdir(path) |
||
| 10 | |||
| 11 | # for each file name
|
||
| 12 | for filename in files: |
||
| 13 | |||
| 14 | # if the file ends with a .wav extension
|
||
| 15 | if filename.endswith('.wav'): |
||
| 16 | |||
| 17 | # create a Sndfile instance for the file
|
||
| 18 | f_in = Sndfile(filename, 'r')
|
||
| 19 | |||
| 20 | # extract the number of frames
|
||
| 21 | numframes = f_in.nframes |
||
| 22 | |||
| 23 | # read all audio samples into 'data'
|
||
| 24 | data = f_in.read_frames(numframes) |
||
| 25 | |||
| 26 | # extract the name (without extension from the file name)
|
||
| 27 | name,extension = os.path.splitext(filename) |
||
| 28 | |||
| 29 | # create a new filename with a .aiff extension
|
||
| 30 | new_filename = name + '.aiff'
|
||
| 31 | |||
| 32 | # create the new format based on aiff
|
||
| 33 | format = Format('aiff')
|
||
| 34 | |||
| 35 | # create a new Sndfile instance for the output file
|
||
| 36 | f_out = Sndfile(new_filename, 'w', format, f_in.channels, f_in.samplerate)
|
||
| 37 | |||
| 38 | # write out audio samples to the new file
|
||
| 39 | f_out.write_frames(data[:numframes]) |
||
| 40 | |||
| 41 | # close the audio file
|
||
| 42 | f_out.close() |
||
| 43 |