【问题标题】:How to program sound card to output a specific signal?如何对声卡进行编程以输出特定信号?
【发布时间】:2014-03-16 01:18:38
【问题描述】:

我正在尝试对我的声卡进行编程以输出特定值。

假设,我有以下序列

[1,2,3,2,10,4,1,50,20,1]

我想让声卡按照这个顺序输出指定的模拟信号。

我当然可以使用 Windows 多媒体 API。但是,我的任务是轻量级的,我不想使用这么重的框架。

对此有何建议?

【问题讨论】:

    标签: audio hardware multimedia


    【解决方案1】:

    我建议你生成一个 .wav 文件并用媒体播放器播放。

    使用 python 及其wave 模块很容易。在下面的示例中,我使用了 python 3.3

    import wave
    import struct
    
    # define your sequence
    frames = [1,2,3,2,10,4,1,50,20,1]
    
    output = wave.open('out.wav', mode='wb') # create the file that will contain your sequence
    output.setnchannels(1) # 1 for mono
    output.setsampwidth(1) # resolution in number of bytes
    output.setframerate(10) # sampling rate, being usually 44100 Hz
    output.setnframes(len(frames)) # sequence length
    
    for i in range(0, len(frames)):
        output.writeframes(struct.pack('h',frames[i])) # convert to string in hex format
    
    # close the file, you're done
    output.close()
    

    【讨论】:

      【解决方案2】:

      如果您使用 Matlab 或免费的等效软件 Octave,您可以在一行中完成此操作。相关文档为here

      soundsc(x, fs, [ lo, hi ])
          Scale the signal so that [lo, hi] -> [-1, 1], then play it
          at sampling rate fs.  If fs is empty, then the default 8000 Hz
          sampling rate is used.
      

      您在控制台中的函数调用将如下所示...

      soundsc([1,2,3,2,10,4,1,50,20,1], fs, [1 50]);
      

      ... 或者像这样对正整数向量进行手动归一化以给出介于 +/- 1 ... 之间的值 ...

      x = [1,2,3,2,10,4,1,50,20,1];
      x=x-min(x); % get values to range from zero up
      x=x/max(x); % get floating point values to range between 0.0 and 1.0
      x=x*2-1;    % get values to range between +/- 1.0;
      soundsc(x);
      

      【讨论】:

        猜你喜欢
        • 2018-02-11
        • 2012-06-19
        • 1970-01-01
        • 1970-01-01
        • 2016-06-05
        • 2018-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多