首页 > 解决方案 > 如何在 Common Lisp 中的声卡上播放 FM 解调样本?

问题描述

我为 libbladerf 创建了一个 Common Lisp 库绑定,并且可以在 Common Lisp 中接收信号。我有一个解调FM信号的函数,定义如下:


(defun fm-demodulate-sc16-q11-samples (device frequency)
  (configure-channel device (channel-rx 0) frequency 1000 40000000 30)
  (init-sync device)
  (enable-module device (channel-rx 0) t)
  (with-foreign-objects ((rx-samples :uint16 8192)
             (metadata :pointer))
    (let ((status (bladerf_sync_rx (mem-ref device :pointer) rx-samples 4096 metadata 5000))
      (samples-array (make-array 8192 :element-type 'float :initial-element 0.0 :fill-pointer 0)))
      (if (< status 0)
      (error "Failed to recieve IQ samples error: ~S" status)
      (progn
          (loop for i below 4096 by 4
         do
           (let* ((previous-i (mem-aref rx-samples :int16 (1+ i)))
              (previous-q (mem-aref rx-samples :int16 i))
              (current-i  (mem-aref rx-samples :int16 (+ i 3)))
              (current-q  (mem-aref rx-samples :int16 (+ i 2)))
              (previous-complex (complex previous-i previous-q))
              (current-complex (complex current-i current-q))
              (dif-complex (* (conjugate previous-complex) current-complex)))
             (vector-push (atan-complex-number dif-complex) samples-array)))
          samples-array)))))

然后我使用以下代码将其传递给 cl-portaudio:

 (defconstant +frames-per-buffer+ 1024)
 (defconstant +sample-rate+ 44100d0)
 (defconstant +sample-format+ :float)
 (defconstant +num-channels+ 2)

 (defun play-demodulated-fm () 
  (with-audio 
    (with-default-audio-stream (astream +num-channels+ +num-channels+ :sample-format +sample-format+ :sample-rate +sample-rate+ :frames-per-buffer +frames-per-buffer+) 
      (dotimes (i 20)
         (write-stream astream (fm-demodulate-sc16-q11-samples *dev* 863000000)))))

当我播放这个时,我听到的只是一个削波声和以下警告:

array is copied and/or coerced, lisp type is T, requested CFFI type is FLOAT
ALSA lib pcm.c:8432:(snd_pcm_recover) underrun occurred

用 cl-portaudio 播放 FM 解调信号的正确方法是什么?

标签: common-lispsignal-processing

解决方案


推荐阅读