dqhdpppm02183 2015-08-31 20:57
浏览 80
已采纳

PortAudio:默认为每个缓冲区帧的播放延迟

I'm trying to play audio in Go, asynchronously, using PortAudio. As far as I'm aware PortAudio handles its own threading, so I don't need to use any of Go's build-in concurrency stuff. I'm using libsndfile to load the file (also Go bindings). Here is my code:

type Track struct {
    stream   *portaudio.Stream
    playhead int
    buffer   []int32
}

func LoadTrackFilesize(filename string, loop bool, bytes int) *Track {
    // Load file
    var info sndfile.Info
    soundFile, err := sndfile.Open(filename, sndfile.Read, &info)
    if err != nil {
        fmt.Printf("Could not open file: %s
", filename)
        panic(err)
    }
    buffer := make([]int32, bytes)
    numRead, err := soundFile.ReadItems(buffer)
    if err != nil {
        fmt.Printf("Error reading from file: %s
", filename)
        panic(err)
    }
    defer soundFile.Close()

    // Create track
    track := Track{
        buffer: buffer[:numRead],
    }

    // Create stream
    stream, err := portaudio.OpenDefaultStream(
        0, 2, float64(44100), portaudio.FramesPerBufferUnspecified, track.playCallback,
    )
    if err != nil {
        fmt.Printf("Couldn't get stream for file: %s
", filename)
    }
    track.stream = stream

    return &track
}

func (t *Track) playCallback(out []int32) {
    for i := range out {
        out[i] = t.buffer[(t.playhead+i)%len(t.buffer)]
    }
    t.playhead += len(out) % len(t.buffer)
}

func (t *Track) Play() {
    t.stream.Start()
}

Using these functions, after initialising PortAudio and all the rest, plays the audio track I supply - just. It's very laggy, and slows down the rest of my application (a game loop).

However, if I change the frames per buffer value from FramesPerBufferUnspecified to something high, say, 1024, the audio plays fine and doesn't interfere with the rest of my application.

Why is this? The PortAudio documentation suggests that using the unspecified value will 'choose a value for optimum latency', but I'm definitely not seeing that.

Additionally, when playing with this very high value, I notice some tiny artefacts - little 'popping' noises - in the audio.

Is there something wrong with my callback function, or anything else, that could be causing one or both of these problems?

I'm using OSX 10.10.5, with Go 1.3.3 and the libsndfile and portaudio from Homebrew.

Thanks.

  • 写回答

1条回答 默认 最新

  • douji9816 2015-09-01 16:37
    关注

    Moving to the comment to an answer:

    Always test with the latest version of Go.

    Also, @Joel figured out that you need to use float32 instead of int32.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题