Is it possible to analyze with pyaudio the sound of the two microphone channels individually?

Viewed 19

Helo. I am not a Python programmer and it is costing me a lot of sacrifice to understand how certain libraries of this language work.

I have a code that analyzes the amplitude of the microphone and I am using it to recognize the sound of the doorbell. It works fine but I need to do the same with another doorbell (the one in my building). My microphone has two wires with two mics, L and R channels. My idea is to take the L channel to one doorbell and the R channel to the other, and have it analyze the audio from both to detect the loud sound of the doorbell.

Reg_audio.py:

import pyaudio
import math
import struct
import wave
import time
import os

Threshold = 6
SHORT_NORMALIZE = (1.0/32768.0)
chunk = 512
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
swidth = 2
TIMEOUT_LENGTH = 5


class Reg_audio:

    @staticmethod
    def rms(frame):
        count = len(frame) / swidth
        format = "%dh" % (count)
        shorts = struct.unpack(format, frame)

        sum_squares = 0.0
        for sample in shorts:
            n = sample * SHORT_NORMALIZE
            sum_squares += n * n
        rms = math.pow(sum_squares / count, 0.5)

        return rms * 1000

    def __init__(self):
        self.p = pyaudio.PyAudio()
        self.stream = self.p.open(format=FORMAT,
                                  channels=CHANNELS,
                                  rate=RATE,
                                  input=True,
                                  output=True,
                                  frames_per_buffer=chunk)



    def listen(self):
        print('Listening beginning')
        while True:
            input = self.stream.read(chunk)
            rms_val = self.rms(input)
            if rms_val > Threshold:
                print(rms_val)

a = Reg_audio()

a.listen()

The audio output prints if rms_val is greater than the set limit:

9.919247114839035
10.313773318322541
30.080054187359153
53.789355695996036
41.52794474459813
28.528548180867176
24.24695672210991
14.223066142943765
11.55789117008402
12.251714420719331
12.504747321517742
9.625693062362382
10.92589740639538
7.590913645430825

But this is supposed to be the mix of both channels, I need to analyze the L and R independently. Unfortunately I haven't found anything on the net. Setting the CHANNELS variable to 2 doesn't seem to do anything at all, I guess you'll have to separate both channels somewhere in "listen".

0 Answers
Related