0

I edited the following code in order to the estimate location of sound source(drone) using four stereo microphones. The microphones are connected to Rpi 3 through a stereo to usb converter (HF0200C). I dont how can i store the sound of drone onto the pi and secondly this program written by @bilal0302 is givng an errors. I am new to python and raspberry pi so any help will be appreciated.

import pyaudio
import struct
import math
import datetime
import time
import audioop

INITIAL_TAP_THRESHOLD = 200
INITIAL_TAP_THRESHOLD1 = 200
FORMAT = pyaudio.paInt16 
SHORT_NORMALIZE = (1.0/32768.0)
CHANNELS = 1
RATE = 44100  
INPUT_BLOCK_TIME = 0.0001
INPUT_FRAMES_PER_BLOCK = int(RATE*INPUT_BLOCK_TIME)
printed = 1000
printed1 = 1000
timeKonig1 = None
timeKonig = None
detectedmic1 = False
detectedmic2 = False

def get_rms( block ):
    return audioop.rms(block, 2)

def get_rms1( block1 ):
    return audioop.rms(block1, 2)

class TapTester(object):
    def __init__(self):
        self.pa = pyaudio.PyAudio()
        self.stream = self.open_mic_stream()
        self.stream1 = self.open_mic_stream1()
        self.tap_threshold = INITIAL_TAP_THRESHOLD
        self.tap_threshold1 = INITIAL_TAP_THRESHOLD1

    def stop(self):
        self.stream.close()

    def open_mic_stream( self ):

        stream = self.pa.open(   format = FORMAT,
                                 channels = CHANNELS,
                                 rate = RATE,
                                 input = True,
                                 input_device_index = 1,
                                 frames_per_buffer = INPUT_FRAMES_PER_BLOCK)        

        return stream
    def open_mic_stream1( self ):

        stream1 = self.pa.open(  format = FORMAT,
                                 channels = CHANNELS,
                                 rate = RATE,
                                 input = True,
                                 input_device_index = 0,
                                 frames_per_buffer = INPUT_FRAMES_PER_BLOCK)

        return stream1

    def listen(self):
        block = self.stream.read(INPUT_FRAMES_PER_BLOCK, exception_on_overflow = False)
        amplitude = get_rms( block )
        block1 = self.stream1.read(INPUT_FRAMES_PER_BLOCK, exception_on_overflow = False)
        amplitude1 = get_rms( block1 )
        global printed
        if amplitude > self.tap_threshold and printed < i:
            global timeKonig
            timeKonig = datetime.datetime.now()
            global detectedmic1
            detectedmic1 = True
            printed = i + 1000           
        global printed1
        if amplitude1 > self.tap_threshold1 and printed1 < i:
            global timeKonig1
            timeKonig1 = datetime.datetime.now()
            global detectedmic2
            detectedmic2 = True
            printed1 = i + 1000           
        if detectedmic1 and detectedmic2:
            if timeKonig > timeKonig1:
                if (timeKonig - timeKonig1).microseconds < 20000:
                    print "1. ", (timeKonig - timeKonig1).microseconds
            else:
                if timeKonig < timeKonig1:
                    if (timeKonig1 - timeKonig).microseconds < 20000:
                        print "2. ", (timeKonig1 - timeKonig).microseconds
            detectedmic1, detectedmic2 = False, False



if __name__ == "__main__":
    tt = TapTester()

    for i in range(10000000):
        tt.listen()
  • Ask the author, and tell them what errors you have specifically. – Dmitry Grigoryev Jan 7 at 9:45
  • i cannnot contact the author. the program is giving me vague values. I dont know how data is being acquired from both the microphones. I see you have enough reputation to contact him. please help me. thankyou – Karrar Haider Jan 7 at 18:31

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.