<commit_before>import time
import alsaaudio


class AudioInterface:
    """
    Manages the sound interface. This manages the main callback for the audio
    interface and delegates behaviour to the Playback and Recording modules.
    """

    def __init__(self,
                 playback_queue,
                 recording_queue,
                 n_channels,
                 sample_rate,
                 sample_width,
                 output_device_id,
                 input_device_id,
                 frame_count=1024):
        self.playback_queue = playback_queue
        self.recording_queue = recording_queue
        self.n_channels = n_channels
        self.sample_rate = sample_rate
        self.sample_width = sample_width
        self.frame_count = frame_count

        print("frame_count: {}".format(frame_count))

        print("available cards: {}".format(alsaaudio.cards()))
        print("available PCMs: {}".format(alsaaudio.pcms()))

        self.in_stream = alsaaudio.PCM(
            type=alsaaudio.PCM_CAPTURE,
            device=input_device_id)
        self.in_stream.setchannels(2)
        self.in_stream.setrate(self.sample_rate)
        self.in_stream.setformat(alsaaudio.PCM_FORMAT_S16_LE)
        self.in_stream.setperiodsize(self.frame_count)
        print("in_stream card: {}".format(self.in_stream.cardname()))

        self.out_stream = alsaaudio.PCM(
            type=alsaaudio.PCM_PLAYBACK,
            device=output_device_id)
        self.out_stream.setchannels(self.n_channels)
        self.out_stream.setrate(self.sample_rate)
        self.out_stream.setformat(alsaaudio.PCM_FORMAT_S16_LE)
        self.out_stream.setperiodsize(self.frame_count)
        print("out_stream card: {}".format(self.out_stream.cardname()))


        print("Finished initialising audio")


    def start_stream(self):
        pass


    def close_stream(self):
        pass


    def is_active(self):
        return True

    def run(self):
        while True:
            st = time.time()

            # Send recording data
            if self.recording_queue is not None:
                pass
                in_data = self.in_stream.read()
                self.recording_queue.put(in_data)

            # Get output audio
            samples = self.playback_queue.get()

            te = time.time() - st
            # print("Time elapsed: {}".format(te))

            st = time.time()
            self.out_stream.write(samples)
            # print("Write time: {}".format(time.time() - st))
<commit_msg>enable nonblocking recording<commit_after>import time
import alsaaudio


class AudioInterface:
    """
    Manages the sound interface. This manages the main callback for the audio
    interface and delegates behaviour to the Playback and Recording modules.
    """

    def __init__(self,
                 playback_queue,
                 recording_queue,
                 n_channels,
                 sample_rate,
                 sample_width,
                 output_device_id,
                 input_device_id,
                 frame_count=1024):
        self.playback_queue = playback_queue
        self.recording_queue = recording_queue
        self.n_channels = n_channels
        self.sample_rate = sample_rate
        self.sample_width = sample_width
        self.frame_count = frame_count

        print("frame_count: {}".format(frame_count))

        print("available cards: {}".format(alsaaudio.cards()))
        print("available PCMs: {}".format(alsaaudio.pcms()))

        self.in_stream = alsaaudio.PCM(
            type=alsaaudio.PCM_CAPTURE,
            mode=alsaaudio.PCM_NONBLOCK,
            device=input_device_id)
        self.in_stream.setchannels(2)
        self.in_stream.setrate(self.sample_rate)
        self.in_stream.setformat(alsaaudio.PCM_FORMAT_S16_LE)
        self.in_stream.setperiodsize(self.frame_count)
        print("in_stream card: {}".format(self.in_stream.cardname()))

        self.out_stream = alsaaudio.PCM(
            type=alsaaudio.PCM_PLAYBACK,
            device=output_device_id)
        self.out_stream.setchannels(self.n_channels)
        self.out_stream.setrate(self.sample_rate)
        self.out_stream.setformat(alsaaudio.PCM_FORMAT_S16_LE)
        self.out_stream.setperiodsize(self.frame_count)
        print("out_stream card: {}".format(self.out_stream.cardname()))


        print("Finished initialising audio")


    def start_stream(self):
        pass


    def close_stream(self):
        pass


    def is_active(self):
        return True

    def run(self):
        while True:
            st = time.time()

            # Send recording data
            if self.recording_queue is not None:
                pass
                in_data = self.in_stream.read()
                if in_data[0]:
                     self.recording_queue.put(in_data)

            # Get output audio
            samples = self.playback_queue.get()

            te = time.time() - st
            # print("Time elapsed: {}".format(te))

            st = time.time()
            self.out_stream.write(samples)
            # print("Write time: {}".format(time.time() - st))

{'fraction_non_alphanumeric': 0.09264305177111716, 'fraction_numerical': 0.0036979369404437526, 'mean_word_length': 2.019388954171563, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4718393', 'n_tokens_mistral': 1508, 'n_tokens_neox': 1432, 'n_words': 263}