【问题标题】:How do I get a list of my device's audio sample rates using PyAudio or PortAudio?如何使用 PyAudio 或 PortAudio 获取设备的音频采样率列表?
【发布时间】:2011-06-05 03:30:25
【问题描述】:

我想查询我的音频设备并获取其所有可用的采样率。我正在使用 Python 2.6 的 Ubuntu 机器上运行在 PortAudio v19 之上的 PyAudio 0.2。

【问题讨论】:

    标签: audio portaudio pyaudio


    【解决方案1】:

    直接使用Portaudio可以运行以下命令:

    for (int i = 0, end = Pa_GetDeviceCount(); i != end; ++i) {
        PaDeviceInfo const* info = Pa_GetDeviceInfo(i);
        if (!info) continue;
        printf("%d: %s\n", i, info->name);
    }
    

    感谢另一个帖子

    【讨论】:

    【解决方案2】:

    在 pyaudio 发行版中,test/system_info.py 显示了如何确定设备支持的采样率。请参阅section that starts at line 49

    简而言之,您使用PyAudio.is_format_supported 方法,例如

    
    devinfo = p.get_device_info_by_index(1)  # Or whatever device you care about.
    if p.is_format_supported(44100.0,  # Sample rate
                             input_device=devinfo['index'],
                             input_channels=devinfo['maxInputChannels'],
                             input_format=pyaudio.paInt16):
      print 'Yay!'
    

    【讨论】:

      【解决方案3】:

      使用sounddevice 模块,您可以这样做:

      import sounddevice as sd
      
      samplerates = 32000, 44100, 48000, 96000, 128000
      device = 0
      
      supported_samplerates = []
      for fs in samplerates:
          try:
              sd.check_output_settings(device=device, samplerate=fs)
          except Exception as e:
              print(fs, e)
          else:
              supported_samplerates.append(fs)
      print(supported_samplerates)
      

      当我尝试这个时,我得到了:

      32000 Invalid sample rate
      128000 Invalid sample rate
      [44100, 48000, 96000]
      

      您还可以检查是否支持特定数量的通道或特定数据类型。 有关更多详细信息,请查看文档:check_output_settings()。 您当然也可以使用check_input_settings() 来检查设备是否是受支持的输入设备

      如果您不知道设备 ID,请查看 query_devices()

      我认为这仍然无关紧要,但这也适用于 Python 2.6,您只需从 print 语句中删除括号并将 except Exception as e: 替换为 except Exception, e:

      【讨论】:

        猜你喜欢
        • 2014-01-12
        • 1970-01-01
        • 2011-05-28
        • 1970-01-01
        • 1970-01-01
        • 2016-03-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多