5

I am working on a group project. We are looking to develop a program that can receive audio and compare it to a saved audio file and output a audio message if the input and saved files match.

We would like to compare audio files with some sort of python method, but we haven't been able to find any way to do this. We are looking for a library of some sort or another to be able to take data from each file and see if they are similar.

Can't figure out how to get started. If someone can help us get started or point us in the right direction, I think we can take it from there.

We have watched a few dozen tutorials, searched the web and still need some major help. Could someone explain to us how to get started?

CoderBoy
  • 107
  • 11
Tony Butler
  • 59
  • 1
  • 1
  • 2
  • Amazon sells device Alexa which gets speech, sends to Amazon server and gets it back as text. Someone created in Python program which uses Amazon server to recognize speech (and it can be run on Raspberry Pi) – furas Nov 29 '17 at 01:53

1 Answers1

4

Python has a lot of ways to do this. I think you can just compare the files without an extensive audio library. I could be wrong. Otherwise, look into the struct module to convert your wave files into readable integers. Try this to see if it works.

import wave
w_one = wave.open('file_one', 'r')
w_two = wave.open('file_two', 'r')

if w_one.readframes() == w_two.readframes():
    print('exactly the same')
else:
    print('not a match')

The audio message output is uneccassary and will take a TTS library, so just stick with print. You could just read aloud the results. Tell me if it worked. Right now I'm on mobile, so I can't test it, but it should work. You could also just save yourself saying something, then run it in python using:

import os
os.system('prerecorded message.wav')

Make sure your audio is wave files. I hope I was of help. Have fun at the science fair!

If you want it to be if the audio files are similar, you'll have to take each frame and set a range in frequencies that they can be apart, then compare starting at all points in the file. Then you'll have to make a recursive for loop that breaks when it's not in range and starts from the next point in the file. That would be one heck of a project, good luck! You may also create a noise reducing algorithm for background noise, which would be some complicated math. If you want to see if they're

an exact match, use the above code. As for streaming the audio, there won't really be a way to get rid of the background noise, so you might want to look into some modules for that... also a saved audio file and a recorded audio file have less then a 1/44000^s (s is time in seconds of the audio) chance of being exactly the same. You may have to just use the Alexa server

It may be hard to get rolling with python, but once you break past the barrier of understanding, you can do some pretty cool things. I just recently made a graphics wrapper for python's turtle, which I programmed to display a flying rocket ship in 3D! That may either sound like Greek to you, or not sound like much, but it's pretty cool to see something you made work so well.

#Comparing them may be easier than I thought
from scipy.io.wavfile import read as wavread
[samplerate, y] = wavread('Comparison.wav')
[samplerate, z] = wavread('Recording.wav')
for x in range(0,samplerate,4): #Slight compression for the program to run faster.
    y1,y2 = [y[x][0], y[x][1]] #y1,y2 are numbers for your in Comparison.wav. Use these to compare to file 2.
    z1,z2 = [z[x][0], z[x][1]] #z1,z2 are numbers for you to compare.

#Install scipy by holding down the window's Button & R. Then type in
    #pip install scipy
#You may have to press enter a few times to get it to work, but overall, be patient. You should be able to figure out how to compare the files from here.
CoderBoy
  • 107
  • 11
  • We are playing a music file from a cell phone into a mic connected to a laptop. The idea is for the program to take the music from the cell phone and compare it to a saved music file and output a phrase if the two are the same. We are doing this in a controlled environment so not worried about back ground noise. – Tony Butler Nov 29 '17 at 23:18
  • And a 3D flying rocket sounds awesome!!! – Tony Butler Nov 29 '17 at 23:20
  • @Tony background noise will always be a factor, even if insignificant. Try what I said earlier. I will edit my answer to work within a range and at any point. You'll have to give me a few. – CoderBoy Nov 30 '17 at 00:00
  • @I added some code to my answer to get specific data from your files. (Make sure to save the recordings and convert them to wav.) Now, you and your daughter should be able to figure out how to compare the files from here and get it working. – CoderBoy Nov 30 '17 at 00:59
  • Awesome @CodeBoy Thx! – Tony Butler Nov 30 '17 at 02:35
  • @Tony No problem. – CoderBoy Nov 30 '17 at 22:58
  • @Tony I made some edits to hopefully take your post off of "On Hold". If I offended you by doing this, I promise that wasn't my intention. – CoderBoy Nov 30 '17 at 23:01
  • @CodeBoy No problem... i didnt do that anyway – Tony Butler Nov 30 '17 at 23:23
  • @CodeBoy Is this pull input from the mic or a file – Tony Butler Nov 30 '17 at 23:38
  • @CodeBoy from scipy.io.wavfile import read as wavread [samplerate, y] = wavread('Comparison.wav') – Tony Butler Nov 30 '17 at 23:38
  • sorry i think i got it – Tony Butler Nov 30 '17 at 23:40
  • @Tony just incase, this set of code opens wav files and returns data. You will have to compare them using your own code. It is your project :) so I want you and your daughter to have fun and give it a shot. I was just helping you get started. – CoderBoy Dec 01 '17 at 20:25