(sampleRate int, channelCount int, format mux.Format, bufferSizeInBytes int)
| 107 | } |
| 108 | |
| 109 | func newContext(sampleRate int, channelCount int, format mux.Format, bufferSizeInBytes int) (*context, chan struct{}, error) { |
| 110 | c := &context{ |
| 111 | channelCount: channelCount, |
| 112 | cond: sync.NewCond(&sync.Mutex{}), |
| 113 | mux: mux.New(sampleRate, channelCount, format), |
| 114 | ready: make(chan struct{}), |
| 115 | } |
| 116 | theContext = c |
| 117 | |
| 118 | go func() { |
| 119 | defer close(c.ready) |
| 120 | |
| 121 | // Open a default ALSA audio device for blocking stream playback |
| 122 | type openError struct { |
| 123 | device string |
| 124 | err C.int |
| 125 | } |
| 126 | var openErrs []openError |
| 127 | var found bool |
| 128 | |
| 129 | for _, name := range deviceCandidates() { |
| 130 | cname := C.CString(name) |
| 131 | defer C.free(unsafe.Pointer(cname)) |
| 132 | if err := C.snd_pcm_open(&c.handle, cname, C.SND_PCM_STREAM_PLAYBACK, 0); err < 0 { |
| 133 | openErrs = append(openErrs, openError{ |
| 134 | device: name, |
| 135 | err: err, |
| 136 | }) |
| 137 | continue |
| 138 | } |
| 139 | found = true |
| 140 | break |
| 141 | } |
| 142 | if !found { |
| 143 | var msgs []string |
| 144 | for _, e := range openErrs { |
| 145 | msgs = append(msgs, fmt.Sprintf("%q: %s", e.device, C.GoString(C.snd_strerror(e.err)))) |
| 146 | } |
| 147 | c.err.TryStore(fmt.Errorf("oto: ALSA error at snd_pcm_open: %s", strings.Join(msgs, ", "))) |
| 148 | return |
| 149 | } |
| 150 | |
| 151 | // TODO: Should snd_pcm_hw_params_set_periods be called explicitly? |
| 152 | const periods = 2 |
| 153 | var periodSize C.snd_pcm_uframes_t |
| 154 | if bufferSizeInBytes != 0 { |
| 155 | periodSize = C.snd_pcm_uframes_t(bufferSizeInBytes / (channelCount * 4 * periods)) |
| 156 | } else { |
| 157 | periodSize = C.snd_pcm_uframes_t(1024) |
| 158 | } |
| 159 | bufferSize := periodSize * periods |
| 160 | if err := c.alsaPcmHwParams(sampleRate, channelCount, &bufferSize, &periodSize); err != nil { |
| 161 | c.err.TryStore(err) |
| 162 | return |
| 163 | } |
| 164 | |
| 165 | go func() { |
| 166 | buf32 := make([]float32, int(periodSize)*channelCount) |
nothing calls this directly
no test coverage detected
searching dependent graphs…