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 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 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