简体   繁体   中英

PyAudio - Latency variations on Windows 10

I'm using PyAudio with an example from PyAudio's website :

import time
import pyaudio

WIDTH = 2
CHANNELS = 1
RATE = 16000

p = pyaudio.PyAudio()

def callback(in_data, frame_count, time_info, status):
    return (in_data, pyaudio.paContinue)

stream = p.open(format=p.get_format_from_width(WIDTH),
                channels=CHANNELS,
                rate=RATE,
                input=True,
                output=True,
                frames_per_buffer=512,
                stream_callback=callback
               )

stream.start_stream()

while stream.is_active():
    time.sleep(5)

stream.stop_stream()
stream.close()

p.terminate()

I have calculated latency (every second) : 潜伏期图

Does anyone know from where these peaks are coming ?

The major operating systems such as Windows, Linux, or Mac, are non-real-time. That means that the operating system can prioritize other applications CPU time over your audio sampling program. This leaves your program to wait for the CPU to choose its program, to get a sample from the sound card.

There are three ways that I have thought about approaching this issue.

  • Disabling Unnecessary Services
  • Using sound card buffering
  • Switching to a real-time operating system (ALSA on Linux is almost there)

One of the biggest bottlenecks in your system will be your Drive activity. This is most often caused by Windows Update, Virus Scanners/Real-Time Protection, Windows Telemetry, and the Windows Indexing Service (Superfetch). Set your Internet Connection to a Metered One so Windows Update does not auto update. Make sure to disable Real-Time Scanning on your Anti-Virus/Anti-Malware Program, and disable Windows Defender in settings. Windows has this "Corprate-Ware" service called Windows Telemetry, it shares your usage information with Microsoft, you can go ahead and disable it in services. Now you do not really want to disable the Windows Indexing Service, but if your machine is slower than a typical 5200rpm HDD, I would recommend that you disable it in the exact same services window.

Some Sound Cards have Hardware Buffers in them, some do not, it all depends. If you know your system has a hardware buffer, it would greatly decrease the amount of dropped samples experienced. You will have to do your own research on sound card hardware buffers, if you are going to use it.

Finally, just switch to a lower latency operating system, such as Linux. It does not have as much garbage being calculated in the background as Windows does. Try Lubuntu with python, and the ALSA audio driver. It should more than fit your need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM