Skip to content

Commit

Permalink
audio/internal/readerdriver: Use ALSA instead of PulseAudio
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi committed Aug 17, 2021
1 parent a29748a commit a6c52b5
Showing 1 changed file with 102 additions and 127 deletions.
229 changes: 102 additions & 127 deletions audio/internal/readerdriver/driver_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,14 @@

package readerdriver

// #cgo pkg-config: libpulse
// #cgo LDFLAGS: -lpulse
// #cgo pkg-config: alsa
//
// #include <pulse/pulseaudio.h>
//
// void ebiten_readerdriver_contextStateCallback(pa_context *context, void *userdata);
// void ebiten_readerdriver_streamWriteCallback(pa_stream *stream, size_t requested_bytes, void *userdata);
// void ebiten_readerdriver_streamStateCallback(pa_stream *stream, void *userdata);
// void ebiten_readerdriver_streamSuccessCallback(pa_stream *stream, void *userdata);
// #include <alsa/asoundlib.h>
import "C"

import (
"fmt"
"runtime"
"time"
"unsafe"
)

Expand All @@ -43,16 +37,17 @@ type context struct {
channelNum int
bitDepthInBytes int

mainloop *C.pa_threaded_mainloop
context *C.pa_context
stream *C.pa_stream
handle *C.snd_pcm_t
supportsPause bool

players *players
}

var theContext *context

const bufferSize = 4096
func alsaError(err C.int) error {
return fmt.Errorf("readerdriver: ALSA error: %s", C.GoString(C.snd_strerror(err)))
}

func NewContext(sampleRate, channelNum, bitDepthInBytes int) (Context, chan struct{}, error) {
ready := make(chan struct{})
Expand All @@ -66,146 +61,126 @@ func NewContext(sampleRate, channelNum, bitDepthInBytes int) (Context, chan stru
}
theContext = c

c.mainloop = C.pa_threaded_mainloop_new()
if c.mainloop == nil {
return nil, nil, fmt.Errorf("readerdriver: pa_threaded_mainloop_new failed")
}
mainloopAPI := C.pa_threaded_mainloop_get_api(c.mainloop)
if mainloopAPI == nil {
return nil, nil, fmt.Errorf("readerdriver: pa_threaded_mainloop_get_api failed")
// Open a default ALSA audio device for blocking stream playback
cname := C.CString("default")
defer C.free(unsafe.Pointer(cname))
if err := C.snd_pcm_open(&c.handle, cname, C.SND_PCM_STREAM_PLAYBACK, 0); err < 0 {
return nil, nil, alsaError(err)
}

contextName := C.CString("pcm-playback")
defer C.free(unsafe.Pointer(contextName))
c.context = C.pa_context_new(mainloopAPI, contextName)
if c.context == nil {
return nil, nil, fmt.Errorf("readerdriver: pa_context_new failed")
periodSize := C.snd_pcm_uframes_t(1024)
bufferSize := periodSize * 2
if err := c.alsaPcmHwParams(sampleRate, channelNum, &bufferSize, &periodSize); err != nil {
return nil, nil, err
}

C.pa_context_set_state_callback(c.context, C.pa_context_notify_cb_t(C.ebiten_readerdriver_contextStateCallback), unsafe.Pointer(c.mainloop))
go func() {
buf32 := make([]float32, int(periodSize)*c.channelNum)
for {
if err := c.readAndWrite(buf32); err != nil {
panic(err)
}
}
}()

return c, ready, nil
}

runtime.LockOSThread()
defer runtime.UnlockOSThread()
C.pa_threaded_mainloop_lock(c.mainloop)
defer C.pa_threaded_mainloop_unlock(c.mainloop)
func (c *context) alsaPcmHwParams(sampleRate, channelNum int, bufferSize, periodSize *C.snd_pcm_uframes_t) error {
var params *C.snd_pcm_hw_params_t
C.snd_pcm_hw_params_malloc(&params)
defer C.free(unsafe.Pointer(params))

if code := C.pa_threaded_mainloop_start(c.mainloop); code != 0 {
return nil, nil, fmt.Errorf("readerdriver: pa_threaded_mainloop_start failed: %s", C.GoString(C.pa_strerror(code)))
if err := C.snd_pcm_hw_params_any(c.handle, params); err < 0 {
return alsaError(err)
}
if code := C.pa_context_connect(c.context, nil, C.PA_CONTEXT_NOAUTOSPAWN, nil); code != 0 {
return nil, nil, fmt.Errorf("readerdriver: pa_context_connect failed: %s", C.GoString(C.pa_strerror(code)))
if err := C.snd_pcm_hw_params_set_access(c.handle, params, C.SND_PCM_ACCESS_RW_INTERLEAVED); err < 0 {
return alsaError(err)
}

// Wait until the context is ready.
for {
contextState := C.pa_context_get_state(c.context)
if C.PA_CONTEXT_IS_GOOD(contextState) == 0 {
return nil, nil, fmt.Errorf("readerdriver: context state is bad")
}
if contextState == C.PA_CONTEXT_READY {
break
}
C.pa_threaded_mainloop_wait(c.mainloop)
if err := C.snd_pcm_hw_params_set_format(c.handle, params, C.SND_PCM_FORMAT_FLOAT_LE); err < 0 {
return alsaError(err)
}

sampleSpecificatiom := C.pa_sample_spec{
format: C.PA_SAMPLE_FLOAT32LE,
rate: C.uint(sampleRate),
channels: C.uchar(channelNum),
if err := C.snd_pcm_hw_params_set_channels(c.handle, params, C.unsigned(channelNum)); err < 0 {
return alsaError(err)
}
var m C.pa_channel_map
switch channelNum {
case 1:
C.pa_channel_map_init_mono(&m)
case 2:
C.pa_channel_map_init_stereo(&m)
if err := C.snd_pcm_hw_params_set_rate_resample(c.handle, params, 1); err < 0 {
return alsaError(err)
}

streamName := C.CString("Playback")
defer C.free(unsafe.Pointer(streamName))
c.stream = C.pa_stream_new(c.context, streamName, &sampleSpecificatiom, &m)
C.pa_stream_set_state_callback(c.stream, C.pa_stream_notify_cb_t(C.ebiten_readerdriver_streamStateCallback), unsafe.Pointer(c.mainloop))
C.pa_stream_set_write_callback(c.stream, C.pa_stream_request_cb_t(C.ebiten_readerdriver_streamWriteCallback), nil)

const defaultValue = 0xffffffff
bufferAttr := C.pa_buffer_attr{
maxlength: defaultValue,
tlength: bufferSize,
prebuf: defaultValue,
minreq: defaultValue,
sr := C.unsigned(sampleRate)
if err := C.snd_pcm_hw_params_set_rate_near(c.handle, params, &sr, nil); err < 0 {
return alsaError(err)
}
var streamFlags C.pa_stream_flags_t = C.PA_STREAM_START_CORKED | C.PA_STREAM_INTERPOLATE_TIMING |
C.PA_STREAM_NOT_MONOTONIC | C.PA_STREAM_AUTO_TIMING_UPDATE |
C.PA_STREAM_ADJUST_LATENCY

if code := C.pa_stream_connect_playback(c.stream, nil, &bufferAttr, streamFlags, nil, nil); code != 0 {
return nil, nil, fmt.Errorf("readerdriver: pa_stream_connect_playback failed: %s", C.GoString(C.pa_strerror(code)))
if err := C.snd_pcm_hw_params_set_buffer_size_near(c.handle, params, bufferSize); err < 0 {
return alsaError(err)
}
if err := C.snd_pcm_hw_params_set_period_size_near(c.handle, params, periodSize, nil); err < 0 {
return alsaError(err)
}
if err := C.snd_pcm_hw_params(c.handle, params); err < 0 {
return alsaError(err)
}
c.supportsPause = C.snd_pcm_hw_params_can_pause(params) == 1
return nil
}

// Wait until the stream is ready.
for {
streamState := C.pa_stream_get_state(c.stream)
if C.PA_STREAM_IS_GOOD(streamState) == 0 {
return nil, nil, fmt.Errorf("readerdriver: stream state is bad")
func (c *context) readAndWrite(buf32 []float32) error {
for i := range buf32 {
buf32[i] = 0
}
c.players.read(buf32)

for len(buf32) > 0 {
n := C.snd_pcm_writei(c.handle, unsafe.Pointer(&buf32[0]), C.snd_pcm_uframes_t(len(buf32)/c.channelNum))
if n == -C.EPIPE {
// Underrun or overrun occurred.
if err := C.snd_pcm_prepare(c.handle); err < 0 {
return alsaError(err)
}
continue
}
if streamState == C.PA_STREAM_READY {
break
if n < 0 {
return alsaError(C.int(n))
}
C.pa_threaded_mainloop_wait(c.mainloop)
buf32 = buf32[int(n)*c.channelNum:]
}

C.pa_stream_cork(c.stream, 0, C.pa_stream_success_cb_t(C.ebiten_readerdriver_streamSuccessCallback), unsafe.Pointer(c.mainloop))

return c, ready, nil
return nil
}

func (c *context) Suspend() error {
C.pa_stream_cork(c.stream, 1, C.pa_stream_success_cb_t(C.ebiten_readerdriver_streamSuccessCallback), unsafe.Pointer(c.mainloop))
return nil
}
if c.supportsPause {
if err := C.snd_pcm_pause(c.handle, 1); err < 0 {
return alsaError(err)
}
return nil
}

func (c *context) Resume() error {
C.pa_stream_cork(c.stream, 0, C.pa_stream_success_cb_t(C.ebiten_readerdriver_streamSuccessCallback), unsafe.Pointer(c.mainloop))
if err := C.snd_pcm_drop(c.handle); err < 0 {
return alsaError(err)
}
return nil
}

//export ebiten_readerdriver_contextStateCallback
func ebiten_readerdriver_contextStateCallback(context *C.pa_context, mainloop unsafe.Pointer) {
C.pa_threaded_mainloop_signal((*C.pa_threaded_mainloop)(mainloop), 0)
}

//export ebiten_readerdriver_streamStateCallback
func ebiten_readerdriver_streamStateCallback(stream *C.pa_stream, mainloop unsafe.Pointer) {
C.pa_threaded_mainloop_signal((*C.pa_threaded_mainloop)(mainloop), 0)
}

//export ebiten_readerdriver_streamSuccessCallback
func ebiten_readerdriver_streamSuccessCallback(stream *C.pa_stream, userdata unsafe.Pointer) {
}

//export ebiten_readerdriver_streamWriteCallback
func ebiten_readerdriver_streamWriteCallback(stream *C.pa_stream, requestedBytes C.size_t, userdata unsafe.Pointer) {
c := theContext

var buf unsafe.Pointer
var buf32 []float32
var bytesToFill C.size_t = bufferSize
for n := int(requestedBytes); n > 0; n -= int(bytesToFill) {
C.pa_stream_begin_write(stream, &buf, &bytesToFill)
if len(buf32) < int(bytesToFill)/4 {
buf32 = make([]float32, bytesToFill/4)
} else {
for i := 0; i < int(bytesToFill)/4; i++ {
buf32[i] = 0
}
func (c *context) Resume() error {
if c.supportsPause {
if err := C.snd_pcm_pause(c.handle, 0); err < 0 {
return alsaError(err)
}
return nil
}

c.players.read(buf32[:bytesToFill/4])

for i := uintptr(0); i < uintptr(bytesToFill/4); i++ {
*(*float32)(unsafe.Pointer(uintptr(buf) + 4*i)) = buf32[i]
try:
if err := C.snd_pcm_resume(c.handle); err < 0 {
if err == -C.EAGAIN {
time.Sleep(100 * time.Millisecond)
goto try
}

C.pa_stream_write(stream, buf, bytesToFill, nil, 0, C.PA_SEEK_RELATIVE)
if err == -C.ENOSYS {
if err := C.snd_pcm_prepare(c.handle); err < 0 {
return alsaError(err)
}
return nil
}
return alsaError(err)
}
return nil
}

0 comments on commit a6c52b5

Please sign in to comment.