def from_wave(cls, filename, wave_filename, compression=None, block_size=256): """Encodes a new AudioFile from an existing .wav file. Takes a filename string, wave_filename string of an existing WaveAudio file and an optional compression level string. Encodes a new audio file from the wave's data at the given filename with the specified compression level and returns a new ShortenAudio object.""" wave = WaveAudio(wave_filename) if (wave.bits_per_sample() not in (8, 16)): raise UnsupportedBitsPerSample() (head, tail) = wave.pcm_split() if (len(tail) > 0): blocks = [head, None, tail] else: blocks = [head, None] import audiotools.encoders try: audiotools.encoders.encode_shn(filename=filename, pcmreader=wave.to_pcm(), block_size=block_size, verbatim_chunks=blocks) return cls(filename) except IOError, err: cls.__unlink__(filename) raise EncodingError(str(err))
def from_wave(cls, filename, wave_filename, compression=None, block_size=256, progress=None): """encodes a new AudioFile from an existing .wav file takes a filename string, wave_filename string of an existing WaveAudio file and an optional compression level string encodes a new audio file from the wave's data at the given filename with the specified compression level and returns a new ShortenAudio object""" wave = WaveAudio(wave_filename) if wave.bits_per_sample() not in (8, 16): raise UnsupportedBitsPerSample(filename, wave.bits_per_sample()) (head, tail) = wave.pcm_split() from .encoders import encode_shn try: if len(tail) == 0: encode_shn( filename=filename, pcmreader=to_pcm_progress(wave, progress), is_big_endian=False, signed_samples=wave.bits_per_sample() == 16, header_data=head, block_size=block_size, ) else: encode_shn( filename=filename, pcmreader=to_pcm_progress(wave, progress), is_big_endian=False, signed_samples=wave.bits_per_sample() == 16, header_data=head, footer_data=tail, block_size=block_size, ) return cls(filename) except IOError, err: cls.__unlink__(filename) raise EncodingError(str(err))
def from_wave(cls, filename, wave_filename, compression=None, block_size=256, progress=None): """Encodes a new AudioFile from an existing .wav file. Takes a filename string, wave_filename string of an existing WaveAudio file and an optional compression level string. Encodes a new audio file from the wave's data at the given filename with the specified compression level and returns a new ShortenAudio object.""" wave = WaveAudio(wave_filename) if (wave.bits_per_sample() not in (8, 16)): raise UnsupportedBitsPerSample(filename, wave.bits_per_sample()) (head, tail) = wave.pcm_split() if (len(tail) > 0): blocks = [head, None, tail] else: blocks = [head, None] import audiotools.encoders try: audiotools.encoders.encode_shn( filename=filename, pcmreader=to_pcm_progress(wave, progress), block_size=block_size, file_type={8: 2, 16: 5}[wave.bits_per_sample()], verbatim_chunks=blocks) return cls(filename) except IOError, err: cls.__unlink__(filename) raise EncodingError(str(err))
def from_wave(cls, filename, wave_filename, compression=None, progress=None): """Encodes a new AudioFile from an existing .wav file. Takes a filename string, wave_filename string of an existing WaveAudio file and an optional compression level string. Encodes a new audio file from the wave's data at the given filename with the specified compression level and returns a new M4AAudio object.""" if (compression is None) or (compression not in cls.COMPRESSION_MODES): compression = __default_quality__(cls.NAME) try: wave = WaveAudio(wave_filename) wave.verify() except InvalidFile: raise EncodingError(u"invalid wave file") if wave.sample_rate > 96000: # convert through PCMConverter if sample rate is too high import tempfile tempwavefile = tempfile.NamedTemporaryFile(suffix=".wav") try: tempwave = WaveAudio.from_pcm( tempwavefile.name, PCMConverter( to_pcm_progress(wave, progress), sample_rate=96000, channels=wave.channels(), channel_mask=wave.channel_mask(), bits_per_sample=wave.bits_per_sample(), ), ) return cls.__from_wave__(filename, tempwave.filename, compression) finally: if os.path.isfile(tempwavefile.name): tempwavefile.close() else: tempwavefile.close_called = True else: return cls.__from_wave__(filename, wave_filename, compression)
def from_wave(cls, filename, wave_filename, compression=None): wave = WaveAudio(wave_filename) if (wave.bits_per_sample() not in (8,16)): raise UnsupportedBitsPerSample() (head,tail) = wave.pcm_split() if (len(tail) > 0): blocks = [head,None,tail] else: blocks = [head,None] import audiotools.encoders try: audiotools.encoders.encode_shn(filename=filename, pcmreader=wave.to_pcm(), block_size=256, verbatim_chunks=blocks) return cls(filename) except IOError: raise EncodingError("shn")