def embed_LSB(carrierFile,payloadFile,outFile):
    """Embed a message in the carrier audio using modified LSB coding
   
    Returns audio data with the embedded payload
        
    Parameters:
        carrierFile - the name of the carrier audio file
        payloadFile - the name of the payload file
        outFile - the name of the file to which the package will be written
    """
    from amm import wavread,wavwrite
    _logInfo("Encoding...")
    
    # read in the carrier file
    samples,params = wavread(carrierFile,bSplit=False)
    samples = samples.tolist()
    sampleLength = len(samples)

    # Read the payload in and store it as a list of integers
    payload = None
    payloadData = None
    try:
        payload = open(payloadFile, 'rb')
        payloadData = bytesToInts(payload.read())
    except (IOerror) as err:
        _logError("Error reading from file '%s'" % payloadFile)
        raise err
    finally:
        if payload: payload.close()
    
    payloadDataLength = len(payloadData)
    payloadSizeData = bitsForInt(payloadDataLength,size=32)
    payloadSizeDataLength = len(payloadSizeData)

    _logDebug("Payload is %d bits" % payloadDataLength)
    _logDebug("That is %f of # of samples" % (float(payloadDataLength)/sampleLength))
    
    # Verify that the payload is reasonably sized
    if (payloadDataLength > (sampleLength - 32)):
        err = InvalidSize("Payload is too large.")
        _logError("Can't embed the payload into the carrier")
        raise err

    # Embed payload size into first 32 bits of package
    for i in xrange(32):
        if (samples[i] % 2 != payloadSizeData[i]):
            samples[i] += 1
    
    # Embed the payload in the carrier
    for i in xrange(payloadDataLength):
        if (samples[i+32] % 2 != payloadData[i]):
            samples[i+32] += 1

    # Write the package to file
    wavwrite(outFile,params,stream=samples)
    
    _logInfo("Success!")
def decode2(packageFile,outFile):
    from amm import wavread
    _logInfo("Decoding...")
    left,right,params = wavread(packageFile)
    sampleRate = params[2]
    sampleLength = params[3]*2

    windowSize = int(floor(sampleLength*0.001/2))
    _logDebug("window size is %d samples" % windowSize)
    
    payloadSizeBits = int(floor(sampleLength*0.001,2))
    _logDebug("First %d bits represent size of payload" % payloadSizeBits)
def decode(packageFile,outFile):
    """Decode the embedded message from the given package using echo coding

    Writes the binary decoded message to a file

    Parameters:
        packageFile - the name of the package audio file
        outFile - the name of the file to which the decoded payload will be written
    """
    from amm import wavread
    _logInfo("Decoding...")
    left,right,params = wavread(packageFile)
    sampleRate = params[2]
    sampleLength = params[3]*2
def embedSS(carrierFile,payloadFile,outFile):
    from amm import wavread,wavwrite

    # read in the carrier file
    left,right,params = wavread(carrierFile)
    sampleRate = params[2]
    sampleLength = params[3]*2
    
    # Read the payload in and store it as a list of zeros and ones
    payload = None
    payloadData = None
    try:
        payload = open(payloadFile, 'rb')
        payloadData = bytesToInts(payload.read())
    except (IOerror) as err:
        _logError("Error reading from file '%s'" % payloadFile)
        raise err
    finally:
        if payload: payload.close()
    
    payloadSize = len(payloadData)
    sizeRatio = (float(payloadSize)/sampleLength)
    _logDebug("Payload is %d bits; that is %f%% of carrier size" % (payloadSize, sizeRatio*100))
    
    # verify that the payload is not too large
    if (sizeRatio > 0.001):
        err = InvalidSize("Payload is too large")
        _logError("Can't embed the payload in the carrier")
        raise err
    
    # calculate window size
    # this is the number of samples that will encode each bit of the payload
    # it is 0.1% the number of samples, divided by 2 because left and right echo
    # must be the same
    windowSize = int(floor(sampleLength*0.001/2))
    _logDebug("Using window size of %d samples" % windowSize)
    
    payloadSizeBits = int(floor(log(sampleLength*0.001,2)) - 1)
    _logDebug("%d bits needed to encode size of payload" % payloadSizeBits)
    
    payloadData = bitsForInt(len(payloadData),size=payloadSizeBits) + payloadData
    
    from apm import FFT,iFFT
    fftl = FFT(left,windowSize)
    fftr = FFT(right,windowSize)
def decode_LSB(packageFile,outFile):
    """Decode the embedded message from the given package using modified LSB coding
        
    Returns the binary embedded message
        
    Parameters:
        packageFile - the name of the package audio file
        outFile - the name of the file to which the decoded payload will be written
    """
    from amm import wavread
    _logInfo("Decoding...")
    samples,params = wavread(packageFile,bSplit=False)
    
    # the first 32 bits encode the size of the payload
    payloadSizeChunk = samples[0:32]
    payloadSize = intForBits([i % 2 for i in payloadSizeChunk])
    _logDebug("Payload size = %d" % payloadSize)
    
    # check that the encoded payload size is not too large
    if (payloadSize > (len(samples) - 32)):
        err = InvalidSize("Specified payload size is too large")
        _logError("Invalid input for decoding")
        raise err
    
    payloadSegment = samples[32:32+payloadSize]
    
    payload = intsToBytes([i % 2 for i in payloadSegment])
    
    out = None
    try:
        out = io.open(outFile,'wb')
        out.write(bytearray(payload))
        out.flush()
    except IOError as err:
        _logError("Error writing out decoded data")
        raise err
    finally:
        if out: out.close()
    
    _logInfo("Success!")
def embed(carrierFile,payloadFile,outFile):
    """Embed a message in the carrier audio using echo coding
    
    Writes the resulting audio data to outFile
    
    Parameters:
        carrierFile - the name of the carrier audio file
        payloadFile - the name of the payload file
        outFile - the file to which the package will be written
    """
    from amm import wavread,wavwrite
    
    # read in the carrier file
    left,right,params = wavread(carrierFile)
    sampleRate = params[2]
    sampleLength = params[3]*2
    
    # Read the payload in and store it as a list of zeros and ones
    payload = None
    payloadData = None
    try:
        payload = open(payloadFile, 'rb')
        payloadData = bytesToInts(payload.read())
    except (IOerror) as err:
        _logError("Error reading from file '%s'" % payloadFile)
        raise err
    finally:
        if payload: payload.close()
    
    payloadSize = len(payloadData)
    sizeRatio = (float(payloadSize)/sampleLength)
    _logDebug("Payload is %d bits; that is %f%% of carrier size" % (payloadSize, sizeRatio*100))
    
    # verify that the payload is not too large
    if (sizeRatio > 0.001):
        err = InvalidSize("Payload is too large")
        _logError("Can't embed the payload in the carrier")
        raise err

    from apm import FFTs,iFFTs

    timeStep = 1.0/sampleRate
    frequencies = np.fft.fftfreq(WINDOWSIZE,timeStep)
    
    leftFFTs = FFTs(left,WINDOWSIZE,timeStep)
    rightFFTs = FFTs(right,WINDOWSIZE,timeStep)
    
    for i in range(len(leftFFTs)):
        if payloadData[i % payloadSize] == 1:
            freqs = FREQ1
        else:
            freqs = FREQ0
        for f in freqs:
            leftFFTs[i][f] = TONE
    
    for i in range(len(rightFFTs)):
        if payloadData[i % payloadSize] == 1:
            freqs = FREQ1
        else:
            freqs = FREQ0
        for f in freqs:
            rightFFTs[i][f] = TONE
    subplot(211)
    stem(x, response)
    ylabel('Amplitude')
    xlabel(r'n (samples)')
    title(r'Impulse response')
    subplot(212)
    step = cumsum(response)
    stem(x, step)
    ylabel('Amplitude')
    xlabel(r'n (samples)')
    title(r'Step response')
    subplots_adjust(hspace=0.5)


# Read in audio file
samples,params = wavread("almostjungle1.wav",bSplit=False)
samples = samples.tolist()
sampleLength = len(samples)
sampleRate = params[2]


filter_order = 20
coefficients = signal.firwin(filter_order, cutoff=0.3, window="hamming")
#Frequency and phase response
#mfreqz(a)
#show()
#Impulse and step response
#figure(2)
#impz(a)
#show()