# Path: audio/get_freq.py
# def get_freq(nogui = False):
#     frame_rate = 48000.0
#     infile = "test.wav"
#     num_samples = 48000
# 
#     wav_file = wave.open(infile, 'r')
#     data = wav_file.readframes(num_samples)
#     wav_file.close()
# 
#     data = struct.unpack('{n}h'.format(n=num_samples), data)
#     data = np.array(data)
# 
#     data_fft = np.fft.fft(data)
# 
#     # This will give us the frequency we want
#     frequencies = np.abs(data_fft)
#     print("The frequency is {} Hz".format(np.argmax(frequencies)))
# 
#     if nogui:
#         return np.argmax(frequencies)
#     else:
#         plt.subplot(2,1,1)
#         plt.plot(data[:300])
#         plt.title("Original audio wave")
#         plt.subplot(2,1,2)
#         plt.plot(frequencies)
#         plt.title("Frequencies found")
# 
#         plt.xlim(0,1200)
#         plt.savefig('wave.png')
# 
#         plt.show()
# 
# Path: audio/create_wave.py
# def create_wav():
# 
# Path: audio/noisy.py
# def noisy(nogui = False):
#     # frequency is the number of times a wave repeats a second
#     frequency = 1000
#     noisy_freq = 50
#     num_samples = 48000
# 
#     # The sampling rate of the analog to digital convert
#     sampling_rate = 48000.0
# 
#     #Create the sine wave and noise
#     sine_wave = [np.sin(2 * np.pi * frequency * x1 / sampling_rate) for x1 in range(num_samples)]
# 
#     sine_noise = [np.sin(2 * np.pi * noisy_freq * x1/  sampling_rate) for x1 in range(num_samples)]
# 
# 
#     #Convert them to numpy arrays
#     sine_wave = np.array(sine_wave)
#     sine_noise = np.array(sine_noise)
# 
#     # Add them to create a noisy signal
#     combined_signal = sine_wave + sine_noise
#     if not nogui:
# 
#         plt.subplot(3,1,1)
#         plt.title("Original sine wave")
# 
#         # Need to add empty space, else everything looks scrunched up!
#         plt.subplots_adjust(hspace=.5)
#         plt.plot(sine_wave[:500])
# 
#         plt.subplot(3,1,2)
#         plt.title("Noisy wave")
#         plt.plot(sine_noise[:4000])
# 
#         plt.subplot(3,1,3)
#         plt.title("Original + Noise")
#         plt.plot(combined_signal[:3000])
# 
#         plt.show()
# 
# 
#     data_fft = np.fft.fft(combined_signal)
# 
#     freq = (np.abs(data_fft[:len(data_fft)]))
# 
#     if not nogui:
#         plt.plot(freq)
#         plt.title("Before filtering: Will have main signal (1000Hz) + noise frequency (50Hz)")
#         plt.xlim(0,1200)
#         plt.show()
# 
#     filtered_freq = []
#     index = 0
#     result = 0,0
#     for f in freq:
#         # Filter between lower and upper limits
#         # Choosing 950, as closest to 1000. In real world, won't get exact numbers like these
#         if index > 950 and index < 1050:
#             # Has a real value. I'm choosing >1, as many values are like 0.000000001 etc
#             if f > 1:
#                 filtered_freq.append(f)
#                 result = f,index
# 
#             else:
#                 filtered_freq.append(0)
#         else:
#             filtered_freq.append(0)
#         index += 1
# 
#     if not nogui:
#         plt.plot(filtered_freq)
#         plt.title("After filtering: Main signal only (1000Hz)")
#         plt.xlim(0,1200)
#         plt.show()
#         plt.close()
# 
#     recovered_signal = np.fft.ifft(filtered_freq)
# 
#     if not nogui:
# 
#         plt.subplot(3,1,1)
#         plt.title("Original sine wave")
#         # Need to add empty space, else everything looks scrunched up!
#         plt.subplots_adjust(hspace=.5)
# 
#         plt.plot(sine_wave[:500])
# 
#         plt.subplot(3,1,2)
#         plt.title("Noisy wave")
#         plt.plot(combined_signal[:4000])
# 
#         plt.subplot(3,1,3)
#         plt.title("Sine wave after clean up")
#         plt.plot((recovered_signal[:500]))
#     else:
#         return result


from audio import get_freq,create_wave,noisy
import os

def test_create_wave():
    try:
        for f in glob.glob("test.wav"):
            os.remove(f)
    except:
        pass

# Path: audio/get_freq.py
# def get_freq(nogui = False):
#     frame_rate = 48000.0
#     infile = "test.wav"
#     num_samples = 48000
# 
#     wav_file = wave.open(infile, 'r')
#     data = wav_file.readframes(num_samples)
#     wav_file.close()
# 
#     data = struct.unpack('{n}h'.format(n=num_samples), data)
#     data = np.array(data)
# 
#     data_fft = np.fft.fft(data)
# 
#     # This will give us the frequency we want
#     frequencies = np.abs(data_fft)
#     print("The frequency is {} Hz".format(np.argmax(frequencies)))
# 
#     if nogui:
#         return np.argmax(frequencies)
#     else:
#         plt.subplot(2,1,1)
#         plt.plot(data[:300])
#         plt.title("Original audio wave")
#         plt.subplot(2,1,2)
#         plt.plot(frequencies)
#         plt.title("Frequencies found")
# 
#         plt.xlim(0,1200)
#         plt.savefig('wave.png')
# 
#         plt.show()
# 
# Path: audio/create_wave.py
# def create_wav():
# 
# Path: audio/noisy.py
# def noisy(nogui = False):
#     # frequency is the number of times a wave repeats a second
#     frequency = 1000
#     noisy_freq = 50
#     num_samples = 48000
# 
#     # The sampling rate of the analog to digital convert
#     sampling_rate = 48000.0
# 
#     #Create the sine wave and noise
#     sine_wave = [np.sin(2 * np.pi * frequency * x1 / sampling_rate) for x1 in range(num_samples)]
# 
#     sine_noise = [np.sin(2 * np.pi * noisy_freq * x1/  sampling_rate) for x1 in range(num_samples)]
# 
# 
#     #Convert them to numpy arrays
#     sine_wave = np.array(sine_wave)
#     sine_noise = np.array(sine_noise)
# 
#     # Add them to create a noisy signal
#     combined_signal = sine_wave + sine_noise
#     if not nogui:
# 
#         plt.subplot(3,1,1)
#         plt.title("Original sine wave")
# 
#         # Need to add empty space, else everything looks scrunched up!
#         plt.subplots_adjust(hspace=.5)
#         plt.plot(sine_wave[:500])
# 
#         plt.subplot(3,1,2)
#         plt.title("Noisy wave")
#         plt.plot(sine_noise[:4000])
# 
#         plt.subplot(3,1,3)
#         plt.title("Original + Noise")
#         plt.plot(combined_signal[:3000])
# 
#         plt.show()
# 
# 
#     data_fft = np.fft.fft(combined_signal)
# 
#     freq = (np.abs(data_fft[:len(data_fft)]))
# 
#     if not nogui:
#         plt.plot(freq)
#         plt.title("Before filtering: Will have main signal (1000Hz) + noise frequency (50Hz)")
#         plt.xlim(0,1200)
#         plt.show()
# 
#     filtered_freq = []
#     index = 0
#     result = 0,0
#     for f in freq:
#         # Filter between lower and upper limits
#         # Choosing 950, as closest to 1000. In real world, won't get exact numbers like these
#         if index > 950 and index < 1050:
#             # Has a real value. I'm choosing >1, as many values are like 0.000000001 etc
#             if f > 1:
#                 filtered_freq.append(f)
#                 result = f,index
# 
#             else:
#                 filtered_freq.append(0)
#         else:
#             filtered_freq.append(0)
#         index += 1
# 
#     if not nogui:
#         plt.plot(filtered_freq)
#         plt.title("After filtering: Main signal only (1000Hz)")
#         plt.xlim(0,1200)
#         plt.show()
#         plt.close()
# 
#     recovered_signal = np.fft.ifft(filtered_freq)
# 
#     if not nogui:
# 
#         plt.subplot(3,1,1)
#         plt.title("Original sine wave")
#         # Need to add empty space, else everything looks scrunched up!
#         plt.subplots_adjust(hspace=.5)
# 
#         plt.plot(sine_wave[:500])
# 
#         plt.subplot(3,1,2)
#         plt.title("Noisy wave")
#         plt.plot(combined_signal[:4000])
# 
#         plt.subplot(3,1,3)
#         plt.title("Sine wave after clean up")
#         plt.plot((recovered_signal[:500]))
#     else:
#         return result


# Path: test_audio.py
from audio import get_freq,create_wave,noisy
import os


def test_create_wave():
    try:
        for f in glob.glob("test.wav"):
            os.remove(f)
    except:
        pass

    create_wave.create_wav()