Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pyAudio playing variable
#1
Hello Guys,

I want play a variable with pyAudio like with:

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

Threshold = 10

SHORT_NORMALIZE = (1.0 / 32768.0)
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
swidth = 2

TIMEOUT_LENGTH = 5

f_name_directory = r'C:\records'


class Recorder:

    @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 record(self):
        print('Noise detected, recording beginning')
        rec = []
        current = time.time()
        end = time.time() + TIMEOUT_LENGTH

        while current <= end:

            data = self.stream.read(chunk)
            if self.rms(data) >= Threshold: end = time.time() + TIMEOUT_LENGTH

            current = time.time()
            rec.append(data)
        # self.write(b''.join(rec))
        self.play(b''.join(rec))


"""
    def write(self, recording):
        n_files = len(os.listdir(f_name_directory))

        filename = os.path.join(f_name_directory, '{}.wav'.format(n_files))

        wf = wave.open(filename, 'wb')
        wf.setnchannels(CHANNELS)
        wf.setsampwidth(self.p.get_sample_size(FORMAT))
        wf.setframerate(RATE)
        wf.writeframes(recording)
        wf.close()
        print('Written to file: {}'.format(filename))
        print('Returning to listening')
"""


    def play(self, recording):
        print('Playing...')
       while len(recording) > 0:
            self.stream.read(recording)
            self.stream.readframes(CHUNK)

        # cleanup stuff.
        self.p.close()
        self.p.terminate()


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


a = Recorder()

a.listen()
The original script cames from Stack Overflow and I want playing the variable without writing it to file (protect HDD healthy).

But I cant get them working...

Anyone can help me?
Reply


Messages In This Thread
pyAudio playing variable - by Talking2442 - Nov-30-2020, 12:35 PM
RE: pyAudio playing variable - by Talking2442 - Dec-01-2020, 12:19 PM
RE: pyAudio playing variable - by snippsat - Dec-01-2020, 02:36 PM
RE: pyAudio playing variable - by Talking2442 - Dec-01-2020, 06:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pyaudio seems to randomly halt input. elpidiovaldez5 2 421 Jan-22-2024, 09:07 AM
Last Post: elpidiovaldez5
  Not getting response from pyaudio OceansBlue 1 2,687 Jul-03-2021, 06:22 AM
Last Post: OceansBlue
  help with PyAudio Leo12143 1 1,986 Jan-18-2021, 09:56 AM
Last Post: DT2000
  Pyaudio Souls99 7 3,613 Oct-05-2020, 04:06 PM
Last Post: Larz60+
  PyAudio [Errorno -9999] Unanticipated Host Error iMuny 5 5,773 Sep-21-2020, 06:58 PM
Last Post: jefsummers
  PyAudio Buffer Data Calculation MclarenF1 0 2,162 Aug-21-2020, 10:55 AM
Last Post: MclarenF1
  PyAudio throwing Input overflowed anthares 3 4,851 Jun-14-2020, 03:37 PM
Last Post: anthares
  Using pyaudio to stop recording under certain sound threshold Twanski94 2 6,531 Jun-13-2020, 11:35 AM
Last Post: Twanski94
  update imhow in callback from pyaudio markB 0 2,374 May-28-2020, 06:01 PM
Last Post: markB
  When piping a FFMPEG stream to PyAudio, I get a "click" on every loop klehman 0 4,891 Dec-15-2019, 04:22 AM
Last Post: klehman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020