tommax
Goto Top

Python Audio-Aufnahme

Hallo liebe Kollegen!

Ich habe leider ein Problem mit dem folgenden Python-Script, das zur Aufzeichnung von Tondateien beim Erreichen eines Schwellenwert von 30 db benutzt wird. Das Script zeichnet erst dann auf, wenn eine Sound duch Mikrophone erkannt wird.
Das Script läuft beim ersten Durchgang ohne Probleme und speichert die wav.Datei in dem angegeben Ordner.
Danach erscheint wieder "Waiting for Speech" aber dann wird kein Stream mehr aufgezeichnet und das Script bleibt hängen !?

Ich weiss leider nicht, woran es liegt !
Vielleicht kann mir jemnad helfen ...

import pyaudio
import math
import struct
import wave

Threshold = 30

SHORT_NORMALIZE = (1.0/32768.0)
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
swidth = 2
Max_Seconds = 10
TimeoutSignal=((RATE / chunk * Max_Seconds) + 2)
silence = True
FileNameTmp = '/home/pi/demo.wav'  
Time=0
all =

def GetStream(chunk):
    return stream.read(chunk)


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 WriteSpeech(WriteData):
    stream.stop_stream()
    stream.close()
    p.terminate()
    wf = wave.open(FileNameTmp, 'wb')  
    wf.setnchannels(CHANNELS)
    wf.setsampwidth(p.get_sample_size(FORMAT))
    wf.setframerate(RATE)
    wf.writeframes(WriteData)
    wf.close()



def KeepRecord(TimeoutSignal, LastBlock):


    all.append(LastBlock)
    for i in range(0, TimeoutSignal):
        try:
            data = GetStream(chunk)
        except:
            continue
        #I chage here (new Ident)
        all.append(data)

    print "end record after timeout";  
    data = ''.join(all)  
    print "write to File";  
    WriteSpeech(data)
    silence = True
    Time=0
    listen(silence,Time)     

def listen(silence,Time):
    print "waiting for Speech"  
    while silence:

        try:

            input = GetStream(chunk)

        except:

            continue


        rms_value = rms(input)

        if (rms_value > Threshold):

            silence=False

            LastBlock=input

            print "I'm Recording...."  
            KeepRecord(TimeoutSignal, LastBlock)

        Time = Time + 1

        if (Time > TimeoutSignal):
            print "Time Out No Speech Detected"  
            sys.exit()

p = pyaudio.PyAudio()

stream = p.open(format = FORMAT,
    channels = CHANNELS,
    rate = RATE,
    input = True,
    output = True,
    frames_per_buffer = chunk)


listen(silence,Time) 


Danke im Voraus für jede Hilfe !

Gruß
tom

Content-Key: 277396

Url: https://administrator.de/contentid/277396

Printed on: April 16, 2024 at 21:04 o'clock