<s> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> import math <EOL> import itertools <EOL> from bitarray import bitarray <EOL> import audiogen <EOL> from audiogen . util import multiply <EOL> from audiogen . util import constant <EOL> MARK_HZ = <NUM_LIT> <EOL> SPACE_HZ = <NUM_LIT> <EOL> BAUD_RATE = <NUM_LIT> <EOL> TWO_PI = <NUM_LIT> * math . pi <EOL> def encode ( binary_data ) : <EOL> '''<STR_LIT>''' <EOL> framed_data = frame ( binary_data ) <EOL> for sample in itertools . chain ( <EOL> audiogen . silence ( <NUM_LIT> ) , <EOL> multiply ( modulate ( framed_data ) , constant ( <NUM_LIT:0.5> ) ) , <EOL> audiogen . silence ( <NUM_LIT> ) , <EOL> ) : <EOL> yield sample <EOL> def modulate ( data ) : <EOL> '''<STR_LIT>''' <EOL> seconds_per_sample = <NUM_LIT:1.0> / audiogen . sampler . FRAME_RATE <EOL> phase , seconds , bits = <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> <EOL> clock = ( x / BAUD_RATE for x in itertools . count ( <NUM_LIT:1> ) ) <EOL> tones = ( MARK_HZ if bit else SPACE_HZ for bit in data ) <EOL> for boundary , frequency in itertools . izip ( clock , tones ) : <EOL> phase_change_per_sample = TWO_PI / ( audiogen . sampler . FRAME_RATE / frequency ) <EOL> while seconds < boundary : <EOL> yield math . sin ( phase ) <EOL> seconds += seconds_per_sample <EOL> phase += phase_change_per_sample <EOL> if phase > TWO_PI : <EOL> phase -= TWO_PI <EOL> bits += <NUM_LIT:1> <EOL> logger . debug ( "<STR_LIT>" % ( bits , <NUM_LIT:1000> * seconds , <NUM_LIT:1000> * bits / BAUD_RATE , <NUM_LIT:1000> * ( seconds - bits / BAUD_RATE ) , bits / seconds ) ) <EOL> def nrzi ( data ) : <EOL> '''<STR_LIT>''' <EOL> current = True <EOL> for bit in data : <EOL> if not bit : <EOL> current = not current <EOL> yield current <EOL> def frame ( stuffed_data ) : <EOL> '''<STR_LIT>''' <EOL> return nrzi ( <EOL> itertools . chain ( <EOL> bitarray ( "<STR_LIT>" ) * <NUM_LIT:20> , <EOL> bitarray ( "<STR_LIT>" ) * <NUM_LIT:100> , <EOL> stuffed_data , <EOL> bitarray ( "<STR_LIT>" ) <EOL> ) <EOL> ) </s>
