class WaveWriter(object) : def __init__(self, filename, samplerate = 44100, channels = 1, format = Format('wav','float32'), ) : self._info = { 'filename' : filename , 'samplerate' : samplerate, 'channels' : channels, 'format' : format, 'frames' : 0, } # TODO: metadata not implemented self._sndfile = Sndfile(filename, 'w', format, channels, samplerate) if not self._sndfile : raise NameError('Sndfile error loading file %s' % filename) def __enter__(self) : return self def __exit__(self, type, value, traceback) : self._sndfile.sync() self._sndfile.close() if value: raise # ???? def write(self, data) : nframes, channels = data.shape assert channels == self._info['channels'] self._sndfile.write_frames(data)
class WaveWriter(object): def __init__( self, filename, samplerate=44100, channels=1, format=Format('wav', 'float32'), ): self._info = { 'filename': filename, 'samplerate': samplerate, 'channels': channels, 'format': format, 'frames': 0, } # TODO: metadata not implemented self._sndfile = Sndfile(filename, 'w', format, channels, samplerate) if not self._sndfile: raise NameError('Sndfile error loading file %s' % filename) def __enter__(self): return self def __exit__(self, type, value, traceback): self._sndfile.sync() self._sndfile.close() if value: raise # ???? def write(self, data): nframes, channels = data.shape assert channels == self._info['channels'] self._sndfile.write_frames(data)
class TelegraphWriter(Telegraph): """Write Morse Code to an Audio File. TelegraphWriter uses SndFile from the scikits.audiolab package to write out audio data, and as such TelegraphWriter can output to whatever formats are available. See the :func:`available_file_formats` and :func:`available_encodings` to determine what audio outputs are available. :param filename: (str) Filename to output to :param audio_format: (str) Audio format :param audio_encoding: (str) Encoding of audio_format :param alphabet: (str) Morse Code alphabet to be used :raise InvalidFormatEncoding: The default format and encoding is ogg vorbis (http://www.vorbis.com). This produces good quality compressed files, but is slower that (say) WAV 16pcm """ def __init__(self, filename, audio_format="ogg", audio_encoding="vorbis", alphabet="international"): super(TelegraphWriter, self).__init__(alphabet) self.__filename = filename self.__audio_format = audio_format self.__audio_encoding = audio_encoding self.__output_formats = available_file_formats() from scikits.audiolab import Format, Sndfile if self.__audio_format not in available_file_formats() or self.__audio_encoding not in available_encodings(self.__audio_format): raise InvalidFormatEncoding(self.__audio_format, self.__audio_encoding) output_format = Format(self.__audio_format, self.__audio_encoding) self.__output_file = Sndfile(self.__filename, 'w', output_format, 1, 44100) def __write_character(self, character): """Write a character to the output file. :param character: (str) Character to be written :raise TypeError: If a single character is not passed :raise CharacterNotFound: if ignore_unknown is set to False and a character is not found """ self.__output_file.write_frames(character) def encode(self, message): """Write a message to the output file. :param message: (str) Message to be written :raise CharacterNotFound: if ignore_unknown is set to False and a character is not found """ super(TelegraphWriter, self)._encode_message(self._clean_message(message), self.__write_character) self.__output_file.sync()
def test_float_frames(self): """ Check nframes can be a float""" rfd, fd, cfilename = open_tmp_file('pysndfiletest.wav') try: # Open the file for writing format = Format('wav', 'pcm16') a = Sndfile(fd, 'rw', format, channels=1, samplerate=22050) tmp = np.random.random_integers(-100, 100, 1000) tmp = tmp.astype(np.short) a.write_frames(tmp) a.seek(0) a.sync() ctmp = a.read_frames(1e2, dtype=np.short) a.close() finally: close_tmp_file(rfd, cfilename)