def lambda_handler()

in infrastructure/function/src/retrieveMergedAudioUrl/lambda_function.py [0:0]


def lambda_handler(event, context):
    # event = json.loads(event);
    print(event)
    bucket, oneAudioObject, otherAudioObject, transactionId  = event['bucket'], event['oneAudioObject'], event['otherAudioObject'], event['transactionId']

    if(isMergedAlready(bucket, transactionId)):
        url = createPresignedUrl(bucket, transactionId + "_merged.wav");
        return {
            'statusCode': 200,
            'body': { 'url' : url }
        }
    
    if(oneAudioObject['Time'] > otherAudioObject['Time']):
        oneAudioObject, otherAudioObject = otherAudioObject, oneAudioObject
    
    durationMillis = getDuration(oneAudioObject['Time'], otherAudioObject['Time']) * 1000
    oneAudioAddr = "/tmp/" + oneAudioObject['Key']
    otherAudioAddre = "/tmp/" + otherAudioObject['Key']
    downloadFile(oneAudioAddr, bucket, oneAudioObject['Key'])
    downloadFile(otherAudioAddre, bucket, otherAudioObject['Key'])
    
    sound1 = AudioSegment.from_wav(oneAudioAddr)
    sound2 = AudioSegment.from_wav(otherAudioAddre)
    
    if(durationMillis > 100):
        paddingSilence = AudioSegment.silent(duration=durationMillis)
        sound2AfterPadding = paddingSilence.append(sound2);
        
        if(sound1.duration_seconds > sound2AfterPadding.duration_seconds):
            combined = sound1.overlay(sound2AfterPadding)
        else:
            combined = sound2AfterPadding.overlay(sound1)
    else:
        if(sound1.duration_seconds > sound2.duration_seconds):
            combined = sound1.overlay(sound2)
        else:
            combined = sound2.overlay(sound1)

    outputWav = "/tmp/" + transactionId + "_merged.wav"
    combined.export(outputWav, format='wav')
    upload_file(outputWav, bucket, transactionId + "_merged.wav")
    url = createPresignedUrl(bucket, transactionId + "_merged.wav");

    return {
        'statusCode': 200,
        'body': { 'url' : url }
    }