"""PyAudio example: Record a few seconds of audio and save to a WAVE file."""

import pyaudio
import wave
import numpy as np
import threading
import time
from Conversor import *

class Receiver:

    def __init__(self, temp):
        self.CHUNK = 5500
        self.FORMAT = pyaudio.paInt16
        self.CHANNELS = 2
        self.RATE = 44100
        self.RECORD_SECONDS = temp
        self.mensagem = ''
        self.flag = 0
        self.ini = 0
        self.fim = 0
        self.tempo = temp

        # use a Blackman window
        self.window = np.blackman( self.CHUNK )

        # Criando instância do pyaudio
        self.p = pyaudio.PyAudio()
        
        #Criando o cinversor
        self.converter = Conversor()

        # Criando Fluxo de midia
        self.stream = self.p.open(format = self.FORMAT,
                            channels = self.CHANNELS,
                            rate = self.RATE,
                            input = True,
                            frames_per_buffer = self.CHUNK,
                            )

        # Array para armazenar audio
        self.frames = []

    def listening(self):
        for i in range(0, int(self.RATE / self.CHUNK * self.RECORD_SECONDS)):
            data = self.stream.read( self.CHUNK )
            self.frames.append(data)

            # write data out to the audio stream
            #self.stream.write(data)

            # unpack the data and times by the hamming window
            indata = np.array(wave.struct.unpack("%dh"%(len(data)/2),\
                                                data))

            # Take the fft and square each value
            fftData=abs(np.fft.rfft(indata))**2

            # find the maximum
            which = fftData[1:].argmax() + 1

            # use quadratic interpolation around the max
            if which != len(fftData)-1:
                y0,y1,y2 = np.log(fftData[which-1:which+2:])
                x1 = (y2 - y0) * .5 / (2 * y1 - y2 - y0)
                # find the frequency and output it
                thefreq = ( which + x1 ) * self.RATE / self.CHUNK
                

                if( thefreq > 2000 and thefreq < 3500):
                    self.mensagem += '0'
                    if(self.tempo == 0.5):
                        return self.mensagem
                elif( thefreq > 8000 and thefreq < 12000 ):
                    self.mensagem += '1'
                    if(self.tempo == 0.5):
                        return self.mensagem
            else:
                thefreq = which * self.RATE / self.CHUNK

                if( thefreq > 2000 and thefreq < 3500 ):
                    self.mensagem += '0'
                    if(self.tempo == 0.5):
                        return self.mensagem
                elif( thefreq > 8000 and thefreq < 12000 ):
                    self.mensagem += '1'
                    if(self.tempo == 0.5):
                        return self.mensagem
        
        print("****Transmition Ended! ****")

        msg = ''
        count = 0
        for bit in self.mensagem:
            msg += bit
            count += 1
            if( count == 7 ):
                msg += ' '
                count = 0
        return msg
        print("msg: ", msg)
        print("Saida em texto: ", self.converter.bin_to_str( msg ))

        self.stream.stop_stream()
        self.stream.close()
        self.p.terminate()
4a3cb1c16b7af9c64529926d0493a94648f7121a
<mask token>


class Receiver:
    <mask token>
    <mask token>

<mask token>


class Receiver:
    <mask token>

    def listening(self):
        for i in range(0, int(self.RATE / self.CHUNK * self.RECORD_SECONDS)):
            data = self.stream.read(self.CHUNK)
            self.frames.append(data)
            indata = np.array(wave.struct.unpack('%dh' % (len(data) / 2), data)
                )
            fftData = abs(np.fft.rfft(indata)) ** 2
            which = fftData[1:].argmax() + 1
            if which != len(fftData) - 1:
                y0, y1, y2 = np.log(fftData[which - 1:which + 2])
                x1 = (y2 - y0) * 0.5 / (2 * y1 - y2 - y0)
                thefreq = (which + x1) * self.RATE / self.CHUNK
                if thefreq > 2000 and thefreq < 3500:
                    self.mensagem += '0'
                    if self.tempo == 0.5:
                        return self.mensagem
                elif thefreq > 8000 and thefreq < 12000:
                    self.mensagem += '1'
                    if self.tempo == 0.5:
                        return self.mensagem
            else:
                thefreq = which * self.RATE / self.CHUNK
                if thefreq > 2000 and thefreq < 3500:
                    self.mensagem += '0'
                    if self.tempo == 0.5:
                        return self.mensagem
                elif thefreq > 8000 and thefreq < 12000:
                    self.mensagem += '1'
                    if self.tempo == 0.5:
                        return self.mensagem
        print('****Transmition Ended! ****')
        msg = ''
        count = 0
        for bit in self.mensagem:
            msg += bit
            count += 1
            if count == 7:
                msg += ' '
                count = 0
        return msg
        print('msg: ', msg)
        print('Saida em texto: ', self.converter.bin_to_str(msg))
        self.stream.stop_stream()
        self.stream.close()
        self.p.terminate()

<mask token>


class Receiver:

    def __init__(self, temp):
        self.CHUNK = 5500
        self.FORMAT = pyaudio.paInt16
        self.CHANNELS = 2
        self.RATE = 44100
        self.RECORD_SECONDS = temp
        self.mensagem = ''
        self.flag = 0
        self.ini = 0
        self.fim = 0
        self.tempo = temp
        self.window = np.blackman(self.CHUNK)
        self.p = pyaudio.PyAudio()
        self.converter = Conversor()
        self.stream = self.p.open(format=self.FORMAT, channels=self.
            CHANNELS, rate=self.RATE, input=True, frames_per_buffer=self.CHUNK)
        self.frames = []

    def listening(self):
        for i in range(0, int(self.RATE / self.CHUNK * self.RECORD_SECONDS)):
            data = self.stream.read(self.CHUNK)
            self.frames.append(data)
            indata = np.array(wave.struct.unpack('%dh' % (len(data) / 2), data)
                )
            fftData = abs(np.fft.rfft(indata)) ** 2
            which = fftData[1:].argmax() + 1
            if which != len(fftData) - 1:
                y0, y1, y2 = np.log(fftData[which - 1:which + 2])
                x1 = (y2 - y0) * 0.5 / (2 * y1 - y2 - y0)
                thefreq = (which + x1) * self.RATE / self.CHUNK
                if thefreq > 2000 and thefreq < 3500:
                    self.mensagem += '0'
                    if self.tempo == 0.5:
                        return self.mensagem
                elif thefreq > 8000 and thefreq < 12000:
                    self.mensagem += '1'
                    if self.tempo == 0.5:
                        return self.mensagem
            else:
                thefreq = which * self.RATE / self.CHUNK
                if thefreq > 2000 and thefreq < 3500:
                    self.mensagem += '0'
                    if self.tempo == 0.5:
                        return self.mensagem
                elif thefreq > 8000 and thefreq < 12000:
                    self.mensagem += '1'
                    if self.tempo == 0.5:
                        return self.mensagem
        print('****Transmition Ended! ****')
        msg = ''
        count = 0
        for bit in self.mensagem:
            msg += bit
            count += 1
            if count == 7:
                msg += ' '
                count = 0
        return msg
        print('msg: ', msg)
        print('Saida em texto: ', self.converter.bin_to_str(msg))
        self.stream.stop_stream()
        self.stream.close()
        self.p.terminate()

<mask token>
import pyaudio
import wave
import numpy as np
import threading
import time
from Conversor import *


class Receiver:

    def __init__(self, temp):
        self.CHUNK = 5500
        self.FORMAT = pyaudio.paInt16
        self.CHANNELS = 2
        self.RATE = 44100
        self.RECORD_SECONDS = temp
        self.mensagem = ''
        self.flag = 0
        self.ini = 0
        self.fim = 0
        self.tempo = temp
        self.window = np.blackman(self.CHUNK)
        self.p = pyaudio.PyAudio()
        self.converter = Conversor()
        self.stream = self.p.open(format=self.FORMAT, channels=self.
            CHANNELS, rate=self.RATE, input=True, frames_per_buffer=self.CHUNK)
        self.frames = []

    def listening(self):
        for i in range(0, int(self.RATE / self.CHUNK * self.RECORD_SECONDS)):
            data = self.stream.read(self.CHUNK)
            self.frames.append(data)
            indata = np.array(wave.struct.unpack('%dh' % (len(data) / 2), data)
                )
            fftData = abs(np.fft.rfft(indata)) ** 2
            which = fftData[1:].argmax() + 1
            if which != len(fftData) - 1:
                y0, y1, y2 = np.log(fftData[which - 1:which + 2])
                x1 = (y2 - y0) * 0.5 / (2 * y1 - y2 - y0)
                thefreq = (which + x1) * self.RATE / self.CHUNK
                if thefreq > 2000 and thefreq < 3500:
                    self.mensagem += '0'
                    if self.tempo == 0.5:
                        return self.mensagem
                elif thefreq > 8000 and thefreq < 12000:
                    self.mensagem += '1'
                    if self.tempo == 0.5:
                        return self.mensagem
            else:
                thefreq = which * self.RATE / self.CHUNK
                if thefreq > 2000 and thefreq < 3500:
                    self.mensagem += '0'
                    if self.tempo == 0.5:
                        return self.mensagem
                elif thefreq > 8000 and thefreq < 12000:
                    self.mensagem += '1'
                    if self.tempo == 0.5:
                        return self.mensagem
        print('****Transmition Ended! ****')
        msg = ''
        count = 0
        for bit in self.mensagem:
            msg += bit
            count += 1
            if count == 7:
                msg += ' '
                count = 0
        return msg
        print('msg: ', msg)
        print('Saida em texto: ', self.converter.bin_to_str(msg))
        self.stream.stop_stream()
        self.stream.close()
        self.p.terminate()

"""PyAudio example: Record a few seconds of audio and save to a WAVE file."""

import pyaudio
import wave
import numpy as np
import threading
import time
from Conversor import *

class Receiver:

    def __init__(self, temp):
        self.CHUNK = 5500
        self.FORMAT = pyaudio.paInt16
        self.CHANNELS = 2
        self.RATE = 44100
        self.RECORD_SECONDS = temp
        self.mensagem = ''
        self.flag = 0
        self.ini = 0
        self.fim = 0
        self.tempo = temp

        # use a Blackman window
        self.window = np.blackman( self.CHUNK )

        # Criando instância do pyaudio
        self.p = pyaudio.PyAudio()
        
        #Criando o cinversor
        self.converter = Conversor()

        # Criando Fluxo de midia
        self.stream = self.p.open(format = self.FORMAT,
                            channels = self.CHANNELS,
                            rate = self.RATE,
                            input = True,
                            frames_per_buffer = self.CHUNK,
                            )

        # Array para armazenar audio
        self.frames = []

    def listening(self):
        for i in range(0, int(self.RATE / self.CHUNK * self.RECORD_SECONDS)):
            data = self.stream.read( self.CHUNK )
            self.frames.append(data)

            # write data out to the audio stream
            #self.stream.write(data)

            # unpack the data and times by the hamming window
            indata = np.array(wave.struct.unpack("%dh"%(len(data)/2),\
                                                data))

            # Take the fft and square each value
            fftData=abs(np.fft.rfft(indata))**2

            # find the maximum
            which = fftData[1:].argmax() + 1

            # use quadratic interpolation around the max
            if which != len(fftData)-1:
                y0,y1,y2 = np.log(fftData[which-1:which+2:])
                x1 = (y2 - y0) * .5 / (2 * y1 - y2 - y0)
                # find the frequency and output it
                thefreq = ( which + x1 ) * self.RATE / self.CHUNK
                

                if( thefreq > 2000 and thefreq < 3500):
                    self.mensagem += '0'
                    if(self.tempo == 0.5):
                        return self.mensagem
                elif( thefreq > 8000 and thefreq < 12000 ):
                    self.mensagem += '1'
                    if(self.tempo == 0.5):
                        return self.mensagem
            else:
                thefreq = which * self.RATE / self.CHUNK

                if( thefreq > 2000 and thefreq < 3500 ):
                    self.mensagem += '0'
                    if(self.tempo == 0.5):
                        return self.mensagem
                elif( thefreq > 8000 and thefreq < 12000 ):
                    self.mensagem += '1'
                    if(self.tempo == 0.5):
                        return self.mensagem
        
        print("****Transmition Ended! ****")

        msg = ''
        count = 0
        for bit in self.mensagem:
            msg += bit
            count += 1
            if( count == 7 ):
                msg += ' '
                count = 0
        return msg
        print("msg: ", msg)
        print("Saida em texto: ", self.converter.bin_to_str( msg ))

        self.stream.stop_stream()
        self.stream.close()
        self.p.terminate()