Exemplo n.º 1
0
def listen(old=0, error_count=0, min_decibel=100, max_decibel=0):
    print("Listening")
    while True:
        try:
            ## read() returns string. You need to decode it into an array later.
            block = stream.read(CHUNK)
        except IOError as e:
            error_count += 1
            print(" (%d) Error recording: %s" % (error_count, e))
        else:
            ## Int16 is a numpy data type which is Integer (-32768 to 32767)
            ## If you put Int8 or Int32, the result numbers will be ridiculous
            decoded_block = numpy.fromstring(block, 'Int16')
            ## This is where you apply A-weighted filter
            y = lfilter(NUMERATOR, DENOMINATOR, decoded_block)
            new_decibel = 20 * numpy.log10(spl.rms_flat(y))
            if is_meaningful(old, new_decibel):
                old = new_decibel
                print('A-weighted: {:+.2f} dB'.format(new_decibel))
                #update_text(SINGLE_DECIBEL_FILE_PATH, '{:.2f} dBA'.format(new_decibel))
                #max_decibel = update_max_if_new_is_larger_than_max(new_decibel, max_decibel)
                #click('update_decibel')

    stream.stop_stream()
    stream.close()
    pa.terminate()
def listen(old=0, error_count=0, min_decibel=100, max_decibel=0):
    print("Listening")
    while True:
        try:
            ## read() returns string. You need to decode it into an array later.
            block = stream.read(CHUNK)
        except IOError as e:
            error_count += 1
            print(" (%d) Error recording: %s" % (error_count, e))
        else:
            ## Int16 is a numpy data type which is Integer (-32768 to 32767)
            ## If you put Int8 or Int32, the result numbers will be ridiculous
            decoded_block = numpy.fromstring(block, 'Int16')
            ## This is where you apply A-weighted filter
            y = lfilter(NUMERATOR, DENOMINATOR, decoded_block)
            new_decibel = 20*numpy.log10(spl.rms_flat(y))
            if is_meaningful(old, new_decibel):
                old = new_decibel
                print('A-weighted: {:+.2f} dB'.format(new_decibel))
                update_text(SINGLE_DECIBEL_FILE_PATH, '{:.2f} dBA'.format(new_decibel))
                max_decibel = update_max_if_new_is_larger_than_max(new_decibel, max_decibel)
                click('update_decibel')


    stream.stop_stream()
    stream.close()
    pa.terminate()
Exemplo n.º 3
0
    def to_decibel(self, data_chunk):

        # Int16 is a numpy data type which is Integer (-32768 to 32767)
        # If you put Int8 or Int32, the result numbers will be ridiculous
        decoded_block = numpy.fromstring(data_chunk, 'Int16')

        # This is where you apply A-weighted filter
        y = lfilter(NUMERATOR, DENOMINATOR, decoded_block)
        new_decibel = 20 * numpy.log10(spl.rms_flat(y))
        return new_decibel
def listen(old=0, error_count=0, min_decibel=100, max_decibel=0):
    try:
        ## read() returns string. You need to decode it into an array later.
        block = stream.read(size, exception_on_overflow=False)
    except IOError as e:
        error_count += 1
        print(" (%d) Error type is: %s" % (error_count, e))
    else:
        ## Int16 is a numpy data type which is Integer (-32768 to 32767)
        ## If you put Int8 or Int32, the result numbers will be ridiculous
        decoded_block = numpy.fromstring(block, 'Int16')
        ## This is where you apply A-weighted filter
        y = lfilter(NUMERATOR, DENOMINATOR, decoded_block)
        new_decibel = 20 * numpy.log10(spl.rms_flat(y))
        if True:
            print('A-weighted: {:+.2f} dB'.format(new_decibel))
            return round(new_decibel, 1)
Exemplo n.º 5
0
def listen(old=0, error_count=0, min_decibel=100, max_decibel=0):
    print("Listening")
    while True:
        try:
            block = stream.read(CHUNK)
        except IOError as e:
            error_count += 1
            print(" (%d) Error recording: %s" % (error_count, e))
        else:

            decoded_block = numpy.fromstring(block, 'Int16')
            y = lfilter(NUMERATOR, DENOMINATOR, decoded_block)
            new_decibel = 20 * numpy.log10(spl.rms_flat(
                y))  #root mean squared of the sound array recorded.
            if is_meaningful(old, new_decibel):
                print('A-weighted: {:+.2f} dB'.format(new_decibel))
                # max_value=read_max_value(MAX_DECIBEL_FILE_PATH)              # edith value in this text file
                # update_max_if_new_is_larger_than_max(new_decibel, max_value)
                try:
                    f = open(
                        '/home/pi/Desktop/Soundmeter/decibel_data/max_decibel.txt',
                        'r')
                except IOError as e:
                    print(e)
                else:
                    value2 = f.read()
                    f.close()
                    print(value2)
                try:
                    noise_limit = int(value2)
                except:
                    noise_limit = 50
                    print("invalid limit")

                if new_decibel > noise_limit:
                    gpio.output(18, gpio.HIGH)
                    print("Higher than limit")
                else:
                    gpio.output(18, gpio.LOW)
                    print("Lower than limit")

    stream.stop_stream()
    stream.close()
    pa.terminate()
Exemplo n.º 6
0
def listen(old=0, error_count=0, min_decibel=100, max_decibel=0):
    while True:
        try:
            ## read() returns string. You need to decode it into an array later.
            block = stream.read(CHUNK)
        except IOError as e:
            error_count += 1
          else:
            ## Int16 is a numpy data type which is Integer (-32768 to 32767)
            ## If you put Int8 or Int32, the result numbers will be ridiculous
            decoded_block = numpy.fromstring(block, 'Int16')
            ## This is where you apply A-weighted filter
            y = lfilter (NUMERATOR, DENOMINATOR, decoded_block)
            new_decibel = (20*numpy.log10(spl.rms_flat(y)))
            return new_decibel

    stream.stop_stream()
    stream.close()
    pa.terminate()
Exemplo n.º 7
0
def listen(old=0, error_coint=0, min_decibel=100, max_decibel=0):
    print("Listening")
    while True:
        try:
            block = stream.read(CHUNK)
        except IOError, e:
            error_count += 1
            print(" (%d) Error recording: %s" % (error_count, e))
        else:
            ## Int16 is a numpy data type which is Integer (-32768 to 32767)
            ## If you put Int8 or Int32, the result numbers will be ridiculous
            decoded_block = numpy.fromstring(block, 'Int16')
            ## This is where you apply A-weighted filter
            y = lfilter(NUMERATOR, DENOMINATOR, decoded_block)
            new_decibel = 20 * numpy.log10(spl.rms_flat(y))
            #print 'new decibels: ', new_decibel
            #return new_decibel

            if spl_is_meaningful(old, new_decibel):
                old = new_decibel
                print('A-weighted: {:+.2f} dB'.format(new_decibel))
Exemplo n.º 8
0
    def __Listen(self, duration):

        endTime = time.time() + duration
        print("Listening...")
        error_count = 0
        while True:
            try:
                block = self.__stream.read(MicUSB.CHUNK,
                                           exception_on_overflow=False)
            except IOError as e:
                error_count += 1
                print(" (%d) Error recording: %s" % (error_count, e))
            else:
                decoded_block = numpy.fromstring(block, 'Int16')
                y = lfilter(MicUSB.NUMERATOR, MicUSB.DENOMINATOR,
                            decoded_block)
                self.__currentdB = 20 * numpy.log10(spl.rms_flat(y))
                #print(new_decibel)

        self.__stream.stop_stream()
        self.__stream.close()
        self.__pa.terminate()
Exemplo n.º 9
0
def listen(old=0, error_count=0, min_decibel=100, max_decibel=0):
    print("Listening")
    while True:
        try:
            block = stream.read(CHUNK)
        except IOError as e:
            error_count += 1
            print(" (%d) Error recording: %s" % (error_count, e))
        else:
            decoded_block = numpy.fromstring(block, 'Int16')
            y = lfilter(NUMERATOR, DENOMINATOR, decoded_block)
            new_decibel = 20*numpy.log10(spl.rms_flat(y))
            if is_meaningful(old, new_decibel):
                print('A-weighted: {:+.2f} dB'.format(new_decibel))
                #max_value=read_max_value(MAX_DECIBEL_FILE_PATH)              # edith value in this text file
                #update_max_if_new_is_larger_than_max(new_decibel, max_value)
                


    stream.stop_stream()
    stream.close()
    pa.terminate()
Exemplo n.º 10
0
    def getIntensity():

        global canStore

        try:
            ## read() returns string. You need to decode it into an array later.
            block = stream.read(CHUNK, exception_on_overflow=False)
        except IOError as e:
            lableIntensity.config(" (%d) Error recording: %s" % (e))
            lableIntensity.after(100, new_decibel)
        else:
            ## Int16 is a numpy data type which is Integer (-32768 to 32767)
            ## If you put Int8 or Int32, the result numbers will be ridiculous
            decoded_block = numpy.fromstring(block, 'Int16')
            ## This is where you apply A-weighted filter
            y = lfilter(NUMERATOR, DENOMINATOR, decoded_block)
            new_decibel = 20 * numpy.log10(spl.rms_flat(y))
            if canStore:
                store.store(new_decibel)

            lableIntensity.config(
                text='A-weighted: {:+.2f} dB'.format(new_decibel))
            lableIntensity.after(1000, getIntensity)
Exemplo n.º 11
0
def _filter(data, NUMERATOR, DENOMINATOR, unit="dBA"):
    y = lfilter(NUMERATOR, DENOMINATOR, data)
    rms = 20 * numpy.log10(spl.rms_flat(y))
    t = _format_db_string(rms, unit)
    return t, rms
                     frames_per_buffer=CHUNK)
    trace("listening to mic")
    trace('Press Ctrl-C to quit.')
    while interrupted == False:
        t1 = time.time() * 1000
        try:
            block = stream.read(CHUNK)
        except IOError, e:
            print("Error recording: %s" % (e))
        else:
            ## Int16 is a numpy data type which is Integer (-32768 to 32767)
            ## If you put Int8 or Int32, the result numbers will be ridiculous
            decoded_block = numpy.fromstring(block, 'Int16')  #**3
            ## This is where you apply A-weighted filter
            y = lfilter(NUMERATOR, DENOMINATOR, decoded_block)
            dB = 20 * numpy.log10(spl.rms_flat(
                y)) * 3  #/ 1.5 ##added / 1.5 to adjust sensitivity
            #smoothing
            #dB = sma2(dB)
            trace('A-weighted: {:+.2f} dB'.format(dB))

            if (dB > peak):
                peak = dB
                counter = 0

            if (counter % PEAK_DELAY == 0):
                peak = dB
                counter = 0
        # if(counter % PEAK_DELAY / 8 == 0):
        #        threshold = getDBThreshold()
            threshold = getDBThreshold()
            levelMin = threshold - DELTA_LEVEL
Exemplo n.º 13
0
    PEAK_DB = 0


def get_decibels(stream, spl):
    print("Listening")
    try:
        block = stream.read(CHUNK)
    except IOError, e:
        print(" Error recording: %s" % (e))
    else:
        ## Int16 is a numpy data type which is Integer (-32768 to 32767)
        ## If you put Int8 or Int32, the result numbers will be ridiculous
        decoded_block = numpy.fromstring(block, 'Int16')
        ## This is where you apply A-weighted filter
        y = lfilter(NUMERATOR, DENOMINATOR, decoded_block)
        new_decibel = 20 * numpy.log10(spl.rms_flat(y))

        return new_decibel


def main():

    global received_data, PEAK_DB
    current_response = ''
    previous_response = ''
    counter_1_minute = 0
    last_15_readings = []
    average_reading = dict()

    ser.flushInput()
    last_time = int(time.time())
Exemplo n.º 14
0
def mic(error_count=0):
    # This listens to incoming sound, processes the sound, returns db and sends it to the database.
    print("Listening")
    while True:
        try:
            # read() returns string. You need to decode it into an array later.
            block = stream.read(CHUNK)
        except IOError as e:
            error_count += 1
            print(" (%d) Error recording: %s" % (error_count, e))
        else:
            # Int16 is a numpy data type which is Integer (-32768 to 32767)
            # If you put Int8 or Int32, the result numbers will be ridiculous
            decoded_block = numpy.fromstring(block, 'Int16')
            # This is where you apply A-weighted filter
            y = lfilter(NUMERATOR, DENOMINATOR, decoded_block)
            new_decibel = 20 * numpy.log10(spl.rms_flat(y))

            red_limit = 50
            leds = [18, 11, 13, 15, 16]

            GPIO.setmode(GPIO.BOARD)
            GPIO.setwarnings(False)
            GPIO.setup(leds, GPIO.OUT)

            if new_decibel >= red_limit - 40:
                GPIO.output(leds[0], True)
                print("Onder limiet")
            else:
                GPIO.output(leds[0], False)

            if new_decibel >= red_limit - 30:
                GPIO.output(leds[1], True)
                print("onder 20 db")
            else:
                GPIO.output(leds[1], False)

            if new_decibel >= red_limit - 20:
                print('onder 30db')
                GPIO.output(leds[2], True)
            else:
                GPIO.output(leds[2], False)

            if new_decibel >= red_limit - 10:
                print('onder 40 db')
                GPIO.output(leds[3], True)
            else:
                GPIO.output(leds[3], False)

            if new_decibel > red_limit:
                GPIO.output(leds[4], True)
                print("Red")
            else:
                GPIO.output(leds[4], False)

            # print('A-weighted: {:+.2f} dB'.format(new_decibel)) and end db to database
            if new_decibel > red_limit:
                print(new_decibel)
                connect_database.insert_feedbacklamp(
                    new_decibel, connect_database.get_date(),
                    connect_database.get_time())

            GPIO.cleanup()

    stream.stop_stream()
    stream.close()
    pa.terminate()