<s> from __future__ import absolute_import <EOL> from . import consts <EOL> from . import tag <EOL> import wave <EOL> from . import stream <EOL> supportedCodecs = ( <EOL> consts . AudioCodec . MP3 , <EOL> consts . AudioCodec . UncompressedNativeEndian , <EOL> consts . AudioCodec . UncompressedLittleEndian , <EOL> ) <EOL> uncompressed = ( <EOL> consts . AudioCodec . UncompressedNativeEndian , <EOL> consts . AudioCodec . UncompressedLittleEndian , <EOL> ) <EOL> REASON_OK = None <EOL> REASON_EMPTY = '<STR_LIT>' <EOL> def get_header ( stream_or_tag ) : <EOL> if isinstance ( stream_or_tag , list ) : <EOL> assert len ( stream_or_tag ) > <NUM_LIT:0> , '<STR_LIT>' <EOL> return stream_or_tag [ <NUM_LIT:0> ] <EOL> else : <EOL> assert isinstance ( stream_or_tag , tag . TagDefineSound ) , '<STR_LIT>' <EOL> return stream_or_tag <EOL> def reason_unsupported ( stream_or_tag ) : <EOL> header = get_header ( stream_or_tag ) <EOL> is_stream = isinstance ( stream_or_tag , list ) <EOL> if header . soundFormat not in supportedCodecs : <EOL> return '<STR_LIT>' % ( consts . AudioCodec . tostring ( header . soundFormat ) , <EOL> header . soundFormat ) <EOL> if is_stream and len ( stream_or_tag ) == <NUM_LIT:1> : <EOL> return REASON_EMPTY <EOL> return REASON_OK <EOL> def supported ( stream_or_tag ) : <EOL> return reason_unsupported ( stream_or_tag ) is None <EOL> def junk ( stream_or_tag ) : <EOL> return reason_unsupported ( stream_or_tag ) == REASON_EMPTY <EOL> def get_wave_for_header ( header , output ) : <EOL> w = wave . open ( output , '<STR_LIT:w>' ) <EOL> w . setframerate ( consts . AudioSampleRate . Rates [ header . soundRate ] ) <EOL> w . setnchannels ( consts . AudioChannels . Channels [ header . soundChannels ] ) <EOL> w . setsampwidth ( consts . AudioSampleSize . Bits [ header . soundSampleSize ] / <NUM_LIT:8> ) <EOL> return w <EOL> def write_stream_to_file ( stream , output ) : <EOL> header = get_header ( stream ) <EOL> w = None <EOL> if header . soundFormat in uncompressed : <EOL> w = get_wave_for_header ( header , output ) <EOL> for block in stream [ <NUM_LIT:1> : ] : <EOL> block . complete_parse_with_header ( header ) <EOL> if header . soundFormat == consts . AudioCodec . MP3 : <EOL> output . write ( block . mpegFrames ) <EOL> else : <EOL> w . writeframes ( block . data . read ( ) ) <EOL> if w : <EOL> w . close ( ) <EOL> def write_sound_to_file ( st , output ) : <EOL> assert isinstance ( st , tag . TagDefineSound ) <EOL> if st . soundFormat == consts . AudioCodec . MP3 : <EOL> swfs = stream . SWFStream ( st . soundData ) <EOL> seekSamples = swfs . readSI16 ( ) <EOL> output . write ( swfs . read ( ) ) <EOL> elif st . soundFormat in uncompressed : <EOL> w = get_wave_for_header ( st , output ) <EOL> w . writeframes ( st . soundData . read ( ) ) <EOL> w . close ( ) </s>
