Last active
August 29, 2015 14:22
-
-
Save 0x5742/fb99a8e33da964252df1 to your computer and use it in GitHub Desktop.
Drop-in replacement for ossaudiodev that writes its output to a .wav file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| import wave | |
| FILENAME = 'tmp.wav' | |
| AFMT_S16_NE = 16 | |
| AFMT_S16_LE = 16 | |
| AFMT_U8 = 8 | |
| class osswave(): | |
| ''' | |
| Drop-in replacement for ossaudiodev that writes its output to a .wav file. | |
| ''' | |
| @classmethod | |
| def open(cls, mode, filename=FILENAME): | |
| # only 'w' supported for mode thus far. | |
| return cls(wave.open(filename, mode)) | |
| def __init__(self, wave): | |
| self.wave = wave | |
| self.write = self.wave.writeframesraw | |
| def setparameters(self, fmt, chn, rate, dummy): | |
| self.wave.setsampwidth(int(fmt / 8)) # bytes per sample | |
| self.wave.setnchannels(chn) | |
| self.wave.setframerate(rate) | |
| def close(self): | |
| # ''.encode() will produce an empty bytes object in 3.x | |
| # and a blank string in 2.x | |
| self.wave.writeframes(''.encode()) | |
| self.wave.close() | |
| open = osswave.open |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment