Beispiel #1
0
    def record(self, audio_name: str, max_time: int):
        input(f"press any key to begin to record {max_time} seconds voice >> ")

        stream = PyAudio().open(format=paInt16,
                                channels=self.CHANNEL_NUM,
                                rate=self.SAMPLE_RATE,
                                input=True,
                                frames_per_buffer=self.SAMPLE_NUM)
        my_buf = []
        time_start = time.time()
        last_second = 0
        print(f"time: {last_second} s")
        while True:
            duration = time.time() - time_start
            if duration >= max_time:
                break
            if int(duration) != last_second:
                last_second = int(duration)
                print(f"time: {last_second} s")

            string_audio_data = stream.read(self.SAMPLE_NUM)
            my_buf.append(string_audio_data)

        stream.close()
        self._save_wave_file(audio_name, my_buf)
Beispiel #2
0
 def run(self):
     try:
         pa = PyAudio()  
         stream = pa.open(format=paInt16, channels=1, rate=16000, input=True, frames_per_buffer=AudioSrc.RECORD_DATA_LEN)  
         while not self.needStop:  
             string_audio_data = stream.read(AudioSrc.RECORD_DATA_LEN)
             with self.lock:
                 self.data.append(string_audio_data)
                 
             self.limitBufLen()
         
         #    print(len(self.data))         
         
         pa.close(stream)
     except:
         print("AudioSrc is destroyed")
Beispiel #3
0
class Audio:
    def __init__(self):
        self.RATE = 44100
        self.BPM = 100
        self.L1 = (60 / self.BPM * 4) * self.RATE
        self.L2 = self.L1 / 2
        self.L4 = self.L1 / 4
        self.L8 = self.L1 / 8
        self.stream = None

    def open(self):
        self.stream = PyAudio().open(format=paFloat32,
                                     channels=1,
                                     rate=self.RATE,
                                     frames_per_buffer=1024,
                                     output=True)

    def play(self, freq, length):
        t = float(freq) * np.pi * 2 / self.RATE
        s = np.sin(np.arange(length) * t)
        self.stream.write(s.astype(np.float32).tostring())

    def close(self):
        self.stream.close()
Beispiel #4
0
def audio_freq():

    # Значення крайніх нот у моєму випадку (шестиструнна гітара в строї Ре)

    note_min = 60           # Нота До 4-ї октави
    note_max = 71           # Нота Сі 4-ї октави

    sample_freq = 22050     # Частота кадру в герцах

    # Від збільшення цих констант залежить швидкість оновлення частоти.

    frame_size = 2048       # Кількість зразків у кадрі
    frames_per_fft = 16     # Кількість кадрів для середнього значення ШПФ

    samples_per_fft = frame_size * frames_per_fft   # Кількість зразків на ШПФ
    freq_step = sample_freq / samples_per_fft       # Крок частоти

    # Отримання мінімального та максимального показника для наших нот в межах ШПФ.

    def note_to_fftbin(n):
        return 440 * 2.0 ** ((n - 69) / 12.0) / freq_step

    imin = max(0, int(numpy.floor(note_to_fftbin(note_min - 1))))
    imax = min(samples_per_fft, int(numpy.ceil(note_to_fftbin(note_max + 1))))

    # Визначення простору для ШПФ.

    buf = numpy.zeros(samples_per_fft, dtype=numpy.float32)

    # Функція вікна Хеннінга.

    window = 0.5 * (1 - numpy.cos(numpy.linspace(0, 2*numpy.pi, samples_per_fft, False)))

    # Відкриваємо аудіо потік.

    stream = PyAudio().open(format=paInt16,
                            channels=1,
                            rate=sample_freq,
                            input=True,
                            frames_per_buffer=frame_size)

    stream.start_stream()

    # Отримуємо дані, поки потік відкритий.

    while stream.is_active():

        # Оновлюємо буфер та приймаємо нові дані.

        buf[:-frame_size] = buf[frame_size:]
        buf[-frame_size:] = numpy.fromstring(stream.read(frame_size), numpy.int16)

        # Запускаємо ШПФ в буфері в межах вікна.

        fft = numpy.fft.rfft(buf * window)

        # Отримуємо максимально повторювану частоту в діапазоні.

        freq = (numpy.abs(fft[imin:imax]).argmax() + imin) * freq_step

        # Запис відображення ноти (get_note) у файл freqs.txt

        freq_save(get_note(freq))

    # Правильно закриваємо аудіо потік.

    stream.stop_stream()
    stream.close()
    stream.terminate()
Beispiel #5
0
        # sym_q.put((0, 0, 1))
        # sym_q.put('2')
        sym_q.put('0')

        pa = PyAudio()
        stream = pa.open(format=paFloat32,
                         channels=1,
                         rate=args.samplerate,
                         output=True,
                         frames_per_buffer=SOUND_FRAME_SIZE,
                         stream_callback=callback)

        network_proc = Process(target=network_producer,
                               args=(args.samplerate, sym_q, s))
        # sound_proc = Process(target=sound_consumer, args=(sound_pipe,args.samplerate, stream))
        network_proc.start()
        # sound_proc.start()
        while stream.is_active():
            # a = input()
            # a = a.strip()
            # for x in a:
            #     sym_q.put(int(x))
            sleep(0.1)
        pa.close(stream)
        network_proc.terminate()
        s.close()
    except KeyboardInterrupt:
        # stream.close()
        pa.close(stream)
        network_proc.terminate()
        s.close()
Beispiel #6
0
class Activator:
    """Awaits for the keyword ``Jarvis`` and triggers ``initiator`` when heard.

    >>> Activator

    See Also:
        - Creates an input audio stream from a microphone, monitors it, and detects the specified wake word.
        - Once detected, Jarvis triggers the ``listener.listen()`` function with an ``acknowledgement`` sound played.
        - After processing the phrase, the converted text is sent as response to ``initiator()`` with a ``return`` flag.
        - The ``should_return`` flag ensures, the user is not disturbed when accidentally woke up by wake work engine.
    """

    def __init__(self, input_device_index: int = None):
        """Initiates Porcupine object for hot word detection.

        Args:
            input_device_index: Index of Input Device to use.

        See Also:
            - Instantiates an instance of Porcupine object and monitors audio stream for occurrences of keywords.
            - A higher sensitivity results in fewer misses at the cost of increasing the false alarm rate.
            - sensitivity: Tolerance/Sensitivity level. Takes argument or env var ``sensitivity`` or defaults to ``0.5``

        References:
            - `Audio Overflow <https://people.csail.mit.edu/hubert/pyaudio/docs/#pyaudio.Stream.read>`__ handling.
        """
        logger.info(f"Initiating hot-word detector with sensitivity: {env.sensitivity}")
        keyword_paths = [pvporcupine.KEYWORD_PATHS[x] for x in [pathlib.PurePath(__file__).stem]]
        self.input_device_index = input_device_index
        self.recorded_frames = []

        self.py_audio = PyAudio()
        self.detector = pvporcupine.create(
            library_path=pvporcupine.LIBRARY_PATH,
            model_path=pvporcupine.MODEL_PATH,
            keyword_paths=keyword_paths,
            sensitivities=[env.sensitivity]
        )
        self.audio_stream = None

    def open_stream(self) -> NoReturn:
        """Initializes an audio stream."""
        self.audio_stream = self.py_audio.open(
            rate=self.detector.sample_rate,
            channels=1,
            format=paInt16,
            input=True,
            frames_per_buffer=self.detector.frame_length,
            input_device_index=self.input_device_index
        )

    def close_stream(self) -> NoReturn:
        """Closes audio stream so that other listeners can use microphone."""
        self.py_audio.close(stream=self.audio_stream)
        self.audio_stream = None

    def start(self) -> NoReturn:
        """Runs ``audio_stream`` in a forever loop and calls ``initiator`` when the phrase ``Jarvis`` is heard."""
        try:
            while True:
                sys.stdout.write("\rSentry Mode")
                if not self.audio_stream:
                    self.open_stream()
                pcm = struct.unpack_from("h" * self.detector.frame_length,
                                         self.audio_stream.read(num_frames=self.detector.frame_length,
                                                                exception_on_overflow=False))
                self.recorded_frames.append(pcm)
                if self.detector.process(pcm=pcm) >= 0:
                    playsound(sound=indicators.acknowledgement, block=False)
                    self.close_stream()
                    if phrase := listener.listen(timeout=env.timeout, phrase_limit=env.phrase_limit, sound=False):
                        initiator(phrase=phrase, should_return=True)
                        speaker.speak(run=True)
                if flag := support.check_restart():
                    logger.info(f"Restart condition is set to {flag[0]} by {flag[1]}")
                    if flag[1] == "OFFLINE":
                        stop_processes()
                        for _handler in logger.handlers:
                            if isinstance(_handler, logging.FileHandler):
                                logger.removeHandler(hdlr=_handler)
                        handler = custom_handler()
                        logger.info(f"Switching to {handler.baseFilename}")
                        logger.addHandler(hdlr=handler)
                        shared.processes = start_processes()
                    else:
                        stop_processes(func_name=flag[1])
                        shared.processes[flag[1]] = start_processes(flag[1])
                if flag := support.check_stop():
                    logger.info(f"Stopper condition is set to {flag[0]} by {flag[1]}")
                    self.stop()
                    terminator()