Exemplo n.º 1
0
# FOR MAC - built-in mic has device ID 0, USB Audio device has device ID 2
# FOR PI - input audio has device ID 2, mic audio has device ID 3
# Open input stream source
inputStream = audio.open(format=FORMAT,
                         input_device_index=getInputDeviceID(audio),
                         channels=CHANNELS,
                         rate=RATE,
                         input=True,
                         output=False,
                         frames_per_buffer=CHUNK,
                         stream_callback=input_callback)

# Open mic stream souce
micStream = audio.open(format=FORMAT,
                       input_device_index=getMicDeviceID(audio),
                       channels=CHANNELS,
                       rate=RATE,
                       input=True,
                       frames_per_buffer=CHUNK,
                       stream_callback=mic_callback)

inputStream.start_stream()
micStream.start_stream()

while inputStream.is_active():
    print("Beginning listening...")
    time.sleep(LISTEN_SECONDS)
    inputStream.stop_stream()

micStream.stop_stream()
Exemplo n.º 2
0
def calibrate(plot=False):
    os.system(
        "amixer set PCM 80%"
    )  # set the internal pi volume control to 35 (somehow 74% results in 35)
    audio = pyaudio.PyAudio()

    # FOR MAC - built-in mic has device ID 0, USB Audio device has device ID 2
    # FOR PI - input audio has device ID 2, mic audio has device ID 3
    # Open input stream source

    # Open mic stream souce
    micStream = audio.open(format=MIC_FORMAT,
                           input_device_index=getMicDeviceID(audio),
                           channels=1,
                           rate=RATE,
                           input=True,
                           frames_per_buffer=CHUNK,
                           stream_callback=mic_callback)

    inputStream = audio.open(format=INPUT_FORMAT,
                             input_device_index=getInputDeviceID(audio),
                             channels=CHANNELS,
                             rate=RATE,
                             input=True,
                             frames_per_buffer=CHUNK,
                             stream_callback=input_callback)

    micStream.start_stream()
    inputStream.start_stream()

    sd.play(cal_array, cal_fs)
    sd.wait()

    micStream.close()
    audio.terminate()

    print("Finished listening to calibration signal...")
    '''
    for i in range(len(micIntensityData)):
        micIntensityData[i] = micIntensityData[i]*0.65
    '''

    # get linear relationship... get min length first so dimensions match in the linear fit
    minLength = min(len(calibrateIntensityData), len(micIntensityData))

    #scaledDownMinLength = int(0.9*minLength)

    r = np.corrcoef(calibrateIntensityData[0:minLength],
                    micIntensityData[0:minLength])[1:0]
    print("\nr = %s\n" % (str(r)))
    m, b = np.polyfit(calibrateIntensityData[0:minLength],
                      micIntensityData[0:minLength], 1)
    #m *= 1.1

    # save M and B to calibration_parameters.json
    with open('calibration_parameters.json', 'w') as outfile:
        json.dump({'M': m, 'B': b}, outfile, sort_keys=True, indent=4)

    if plot:
        lowerBound = min(calibrateIntensityData)
        upperBound = max(calibrateIntensityData)
        print("Generating graphs...")
        x = np.linspace(lowerBound, upperBound)
        y = []
        for i in range(0, len(x)):
            y.append(m * x[i] + b)

        micTimeValues = getTimeValues(RATE, CHUNK, len(micIntensityData))
        calibrateTimeValues = getTimeValues(RATE, CHUNK,
                                            len(calibrateIntensityData))
        """
        # Mic power over time plot
        micIntensityFig = plt.figure(figsize=(30,4))
        micIntensityFig.suptitle('Mic Intensity over Time', fontsize=14, fontweight='bold')
        plt.plot(micTimeValues, micIntensityData)
        plt.xlabel('Time (s)')
        plt.ylabel('Relative Fraction of Maximum Intensity')

        
        # Calibrate signal power over time plot
        calibrateIntensityFig = plt.figure(figsize=(30,4))
        calibrateIntensityFig.suptitle('Calibrate Signal Intensity over Time', fontsize=14, fontweight='bold')
        plt.plot(calibrateTimeValues, calibrateIntensityData)
        plt.xlabel('Time (s)')
        plt.ylabel('Relative Fraction of Maximum Intensity')
        
        """

        # Mic Pickup Power vs. Input Signal Power graph
        powerFig = plt.figure(figsize=(18, 6))
        powerFig.suptitle('Mic Pickup Intensity vs. Input Signal Intensity',
                          fontsize=14,
                          fontweight='bold')
        powerDataPoints, = plt.plot(calibrateIntensityData[0:minLength],
                                    micIntensityData[0:minLength],
                                    'o',
                                    color="orange",
                                    label="Intensity Data Points")
        bestFit, = plt.plot(x,
                            y,
                            'b-',
                            label=("Best Fit Line\ny=%.4fx+%.4f" % (m, b)))
        plt.xlabel('Relative Fraction of Maximum Intensity')
        plt.ylabel('Relative Fraction of Maximum Intensity')
        plt.legend(handles=[powerDataPoints, bestFit])

        intensitiesFig = plt.figure(figsize=(18, 12))
        intensitiesFig.suptitle('Calibration Graph',
                                fontsize=14,
                                fontweight='bold')

        # First plot mic
        plt.subplot(211)
        plt.plot(micTimeValues, micIntensityData, color="tab:blue")
        plt.ylabel('Microphone Pickup Intensity')

        # Now plot the input on a separate Subplot
        plt.subplot(212)
        plt.plot(calibrateTimeValues,
                 calibrateIntensityData,
                 color="tab:orange")
        plt.xlabel('Time (s)')
        plt.ylabel('System Input Intensity')

        print("Done!")

        intensitiesFig.savefig('./plots/c_mic_and_input_intensity.png')
        powerFig.savefig('./plots/c_calibration_graph.png')

        #plt.show()

    resetCalibrateData()
    return (m, b)
Exemplo n.º 3
0
def calibrate(plot=False):
    # Begin main thread of code
    audio = pyaudio.PyAudio()
    print("DEFAULT DEVICE: ")
    print(audio.get_default_output_device_info())

    # FOR MAC - built-in mic has device ID 0, USB Audio device has device ID 2
    # FOR PI - input audio has device ID 2, mic audio has device ID 3
    # Open input stream source

    # Open mic stream souce
    micStream = audio.open(format=FORMAT,
                           input_device_index=getMicDeviceID(audio),
                           channels=1,
                           rate=RATE,
                           input=True,
                           output=True,
                           frames_per_buffer=CHUNK,
                           stream_callback=mic_callback)

    micStream.start_stream()

    while micStream.is_active():
        print("Beginning calibration signal...")
        time.sleep(RECORD_SECONDS)
        micStream.stop_stream()

    micStream.close()
    audio.terminate()
    print("Finished listening to calibration signal...")

    m, b = np.polyfit(calibratePowerData,
                      micPowerData[0:len(calibratePowerData)], 1)

    # save M and B to calibration_parameters.json
    with open('calibration_parameters.json', 'w') as outfile:
        json.dump({'M': m, 'B': b}, outfile, sort_keys=True, indent=4)

    if plot:
        lowerBound = min(calibratePowerData)
        upperBound = max(calibratePowerData)
        print("Generating graphs...")
        x = np.linspace(lowerBound, upperBound)
        y = []
        for i in range(0, len(x)):
            y.append(m * x[i] + b)

        micTimeValues = getTimeValues(RATE, CHUNK, len(micPowerData))
        # Mic power over time plot
        micPowerFig = plt.figure(figsize=(30, 4))
        micPowerFig.suptitle('Mic Power over Time',
                             fontsize=14,
                             fontweight='bold')
        plt.plot(micTimeValues, micPowerData)
        plt.xlabel('Time (s)')
        plt.ylabel('UNITS')

        calibrateTimeValues = getTimeValues(RATE, CHUNK,
                                            len(calibratePowerData))
        # Calibrate signal power over time plot
        calibratePowerFig = plt.figure(figsize=(30, 4))
        calibratePowerFig.suptitle('Calibrate Signal Power over Time',
                                   fontsize=14,
                                   fontweight='bold')
        plt.plot(calibrateTimeValues, calibratePowerData)
        plt.xlabel('Time (s)')
        plt.ylabel('UNITS')

        # Mic Pickup Power vs. Input Signal Power graph
        powerFig = plt.figure(figsize=(30, 4))
        powerFig.suptitle('Mic Pickup Power vs. Input Signal Power',
                          fontsize=14,
                          fontweight='bold')
        powerDataPoints, = plt.plot(calibratePowerData,
                                    micPowerData[0:len(calibratePowerData)],
                                    'o',
                                    color="orange",
                                    label="Power Data Points")
        bestFit, = plt.plot(x,
                            y,
                            'b-',
                            label=("Best Fit Line\ny=%.4fx+%.4f" % (m, b)))
        plt.xlabel('UNITS')
        plt.ylabel('UNITS')
        plt.legend(handles=[powerDataPoints, bestFit])
        print("Done!")
        plt.show()

    return (m, b)
Exemplo n.º 4
0
def listen(cal_slope, cal_intercept, sensitivity):
    # set the M and B parameters of the calibration graph
    global M,B, LISTEN_ACTIVE
    #M = cal_slope*1.1
    M = cal_slope
    B = cal_intercept
    
    SENSITIVITY = sensitivity
    CHUNK = sensitivityMap[SENSITIVITY]
    
    print("Set the system's sensitivity to %s\n" % (SENSITIVITY))
    
    
    # Begin main thread of code
    audio = pyaudio.PyAudio()
    
    segment.begin()

    # FOR MAC - built-in mic has device ID 0, USB Audio device has device ID 2
    # FOR PI - input audio has device ID 2, mic audio has device ID 3
    # Open input stream source
    inputStream = audio.open(format=INPUT_FORMAT, 
                        input_device_index=getInputDeviceID(audio),
                        channels=CHANNELS,
                        rate=RATE, 
                        input=True,
                        frames_per_buffer=CHUNK,
                        stream_callback=input_callback)


    # Open mic stream souce
    micStream = audio.open(format=MIC_FORMAT, 
                        input_device_index=getMicDeviceID(audio),
                        channels=CHANNELS,
                        rate=RATE, 
                        input=True,
                        frames_per_buffer=CHUNK,
                        stream_callback=mic_callback)

    print("Beginning listening...")
    inputStream.start_stream()
    micStream.start_stream()
    
    LISTEN_ACTIVE = True
    
    # run until LISTEN_ACTIVE is set false by stopListening
    while LISTEN_ACTIVE:
        pass
    
    inputStream.stop_stream()
    micStream.stop_stream()
    micStream.close()
    inputStream.close()
    audio.terminate()

    print("Finished listening")

    # Power data plot
    micPowerTimeVals = getTimeValues(RATE, CHUNK, len(micPowerData))
    expectedMicPowerTimeVals = getTimeValues(RATE/CHUNK, 1, len(expectedMicPowerData))
    inputPowerTimeVals = getTimeValues(RATE/CHUNK, 1, len(inputPowerData))
    micPowerFig = plt.figure(figsize=(18,6))
    micPowerFig.suptitle('Mic Power & Expected Mic Power over Time', fontsize=14, fontweight='bold')
    micPowerPlot, = plt.plot(micPowerTimeVals, 
                             micPowerData,
                             label="Actual Mic Power")
    expMicPowerPlot, = plt.plot(expectedMicPowerTimeVals, 
                                expectedMicPowerData,
                                label="Expected Mic Power")
    inputPowerPlot, = plt.plot(inputPowerTimeVals, 
                                inputPowerData,
                                label="Input Power")
    plt.xlabel('Time (s)')
    plt.ylabel('UNITS')
    plt.legend(handles=[micPowerPlot, expMicPowerPlot, inputPowerPlot])

    # Plot of moving average of power
    avgMicPowerTimeVals = getTimeValues(RATE, CHUNK, len(avgMicPowerData))
    avgExpectedMicPowerTimeVals = getTimeValues(RATE, CHUNK, len(avgExpectedMicPowerData))
    movingAvgFig = plt.figure(figsize=(18,6))
    movingAvgFig.suptitle('Moving Averages of Mic Power & Expected Mic Power over Time', fontsize=14, fontweight='bold')
    avgMicPowerPlot, = plt.plot(avgMicPowerTimeVals, 
                                avgMicPowerData,
                                label="Mic Power Moving Average")
    avgExpMicPowerPlot, = plt.plot(avgExpectedMicPowerTimeVals,
                                   avgExpectedMicPowerData,
                                   label="Expected Mic Power Moving Average")
    plt.xlabel('Time (s)')
    plt.ylabel('UNITS')
    plt.legend(handles=[avgMicPowerPlot, avgExpMicPowerPlot])

    # Plot of scale factor & ratio over time on same plot
    scaleTimeVals = getTimeValues(RATE, CHUNK, len(scaleData))
    ratioTimeVals = getTimeValues(RATE, CHUNK, len(ratioData))
    potTimeVals = getTimeValues(RATE, CHUNK, len(potValData))
    
    scaleRatioFig = plt.figure(figsize=(18,18))
    scaleRatioFig.suptitle('Scale Factor and Avg. Expected Mic Power to Avg Mic Power Ratio over Time', fontsize=14, fontweight='bold')

    # First plot ratio
    plt.subplot(311)
    plt.plot(ratioTimeVals, ratioData, color="tab:red")
    plt.ylabel('Ratio Magnitude')

    # Next plot a line on the threshold
    threshLine = []
    for i in range (0, len(ratioTimeVals)):
        threshLine.append(THRESHOLD)
    plt.plot(ratioTimeVals, threshLine, color="gray", linestyle='dashed')

    # Now plot the scale on a separate Subplot
    plt.subplot(312)
    plt.plot(scaleTimeVals, scaleData, color="tab:blue")
    plt.xlabel('Time (s)')
    plt.ylabel('Scale Magnitude')
    
    plt.subplot(313)
    plt.plot(potTimeVals, potValData, color="tab:green")
    plt.xlabel('Time (s)')
    plt.ylabel('Potentiometer Value Magnitude')

    # plt.show()
    micPowerFig.savefig('./plots/l_intensities_over_time.png')
    movingAvgFig.savefig('./plots/l_moving_avg.png')
    scaleRatioFig.savefig('./plots/l_ratio_etc.png')
    
    resetListenData()