R
R
r1mple2021-10-15 21:41:44
Python
r1mple, 2021-10-15 21:41:44

Why does the program stop executing?

import reqs
import config
reqs.checkReqs()
from AudioCapture import AudioCapturer
from ScreenCapture import ScreenCapturer
import Combine
import threading
import Files
import sys      
import flag

lock = threading.Lock()

stopFlag = flag.Flag()

def onClose():
    while stopFlag.getFlag() == False:
        input('Do you want to stop?')
        stopFlag.changeFlag()
        ac.stop_and_save()

def onReapet():
    while stopFlag.getFlag() == False:
        global ac, sc
        ac = AudioCapturer(recordTime=5)
        sc = ScreenCapturer(recordTime=5)  
        videoThread = threading.Thread(target=sc.record)       
        audioStartThread = threading.Thread(target=ac.start)
        videoThread.start()
        audioStartThread.start()
        videoThread.join()
        audioStartThread.join() 

if __name__ == '__main__':  
    print('To start recording press "R".')
    print('To stop recording press "S".')
    print('Do you want to start? (y/n)', end=' ')
    ans = input()
    if ans == 'y':
        print('Starting...')
        listener = threading.Thread(target=onClose)
        listener.start()
        onReapet()
        print('Done.')
        listener.join()
        Combine.Combiner.combine()
        Combine.Combiner.compress()
        Files.Files.deleteFiles([config.AUDIOPATH, config.OUTPUTPATH, config.VIDEOPATH])
        print(config.SPLITTER)
        print('Output file location: ', config.FINALPATH)
    elif ans == 'n':
        sys.exit()

import pyaudio
import wave
import Files
import config

class AudioCapturer(object):
    def __init__(self, recordTime = config.DEFAULTRECORDTIME):
        self.p = pyaudio.PyAudio()
        
        self.checkConfig(config.SPLITTER)

        self.recordTime = recordTime
        self.CHUNK = 1024
        self.FORMAT = pyaudio.paInt16
        self.INDEXOFINPUTDEVICE = int(Files.Files.getDataFromJson(config.DEVICECONFIGPATH)['indexOfInputDevice'])
        self.CHANNELS = int(Files.Files.getDataFromJson(config.DEVICECONFIGPATH)['numberOfChannels'])
        self.RATE = round(float(Files.Files.getDataFromJson(config.DEVICECONFIGPATH)['rate']))
        
        self.WAVE_OUTPUT_FILENAME = config.AUDIOPATH

        self.frames = []

        self.stream = self.p.open(format=self.FORMAT,
                            channels=self.CHANNELS,
                            rate=self.RATE,
                            input_device_index=self.INDEXOFINPUTDEVICE,
                            frames_per_buffer=self.CHUNK,
                            input=True)

        self.recordFlag = True

    def checkConfig(self, splitter):
        data = Files.Files.getDataFromJson(config.DEVICECONFIGPATH)
        if data['indexOfInputDevice'] == '' or data['numberOfChannels'] == '' or data['rate'] == '':
            for i in range(self.p.get_device_count()):
                print(i, ' - ', self.p.get_device_info_by_index(i)['name'])
            print('\nSelect the device from you want to record the audio:', end=' ')
            id = input()
            device = self.p.get_device_info_by_index(int(id))
            data['indexOfInputDevice'] = str(device['index'])
            data['rate'] = str(device['defaultSampleRate'])
            data['numberOfChannels'] = str(device['maxInputChannels'])
            Files.Files.writeDataToJson(config.DEVICECONFIGPATH, data)
            print(splitter)

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

        self.stop_and_save()

    def stop_and_save(self):
        self.stream.stop_stream()
        self.stream.close()
        self.p.terminate()  
        wf = wave.open(self.WAVE_OUTPUT_FILENAME, 'wb')
        wf.setnchannels(self.CHANNELS)
        wf.setsampwidth(self.p.get_sample_size(self.FORMAT))
        wf.setframerate(self.RATE)
        wf.writeframes(b''.join(self.frames))
        wf.close()

In the onClose function, there is a flag change and sound recording stop, but when I change the flag as a result, in theory the audio stream should be closed and the program should continue, but in reality it turns out that after changing the flag, the program does not reach print('Done.') and just finishes its execution, why is this happening?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question