2015-06-20 16:39:56 +02:00
|
|
|
// Copyright 2015 Hajime Hoshi
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-02-07 08:03:33 +01:00
|
|
|
package audio
|
2015-06-20 16:39:56 +02:00
|
|
|
|
2016-03-04 19:27:06 +01:00
|
|
|
// #cgo LDFLAGS: -lwinmm
|
|
|
|
//
|
|
|
|
// #include <windows.h>
|
|
|
|
// #include <mmsystem.h>
|
|
|
|
//
|
|
|
|
// #define sizeOfWavehdr (sizeof(WAVEHDR))
|
|
|
|
//
|
|
|
|
// MMRESULT waveOutOpen2(HWAVEOUT* waveOut, WAVEFORMATEX* format);
|
|
|
|
import "C"
|
|
|
|
|
2016-03-04 18:25:51 +01:00
|
|
|
import (
|
2016-03-04 19:27:06 +01:00
|
|
|
"fmt"
|
2016-03-04 18:25:51 +01:00
|
|
|
"io"
|
2016-03-13 17:13:11 +01:00
|
|
|
"time"
|
2016-03-04 19:27:06 +01:00
|
|
|
"unsafe"
|
2016-03-04 18:25:51 +01:00
|
|
|
)
|
|
|
|
|
2016-03-04 19:27:06 +01:00
|
|
|
type header struct {
|
|
|
|
buffer unsafe.Pointer
|
|
|
|
bufferSize int
|
|
|
|
waveHdr C.WAVEHDR
|
|
|
|
}
|
|
|
|
|
2016-04-02 19:43:39 +02:00
|
|
|
// TODO: Reduce panics and use errors instead
|
|
|
|
|
2016-03-04 19:27:06 +01:00
|
|
|
func newHeader(waveOut C.HWAVEOUT, bufferSize int) header {
|
|
|
|
// NOTE: This is never freed so far.
|
|
|
|
buf := C.malloc(C.size_t(bufferSize))
|
|
|
|
h := header{
|
|
|
|
buffer: buf,
|
|
|
|
bufferSize: bufferSize,
|
2016-03-09 19:27:51 +01:00
|
|
|
waveHdr: C.WAVEHDR{
|
2016-03-04 19:27:06 +01:00
|
|
|
lpData: C.LPSTR(buf),
|
|
|
|
dwBufferLength: C.DWORD(bufferSize),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// TODO: Need to unprepare to avoid memory leak?
|
|
|
|
if err := C.waveOutPrepareHeader(waveOut, &h.waveHdr, C.sizeOfWavehdr); err != C.MMSYSERR_NOERROR {
|
|
|
|
panic(fmt.Sprintf("audio: waveOutPrepareHeader error %d", err))
|
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *header) Write(waveOut C.HWAVEOUT, data []byte) {
|
|
|
|
if len(data) != h.bufferSize {
|
|
|
|
panic("audio: len(data) must equal to h.bufferSize")
|
|
|
|
}
|
|
|
|
C.memcpy(h.buffer, unsafe.Pointer(&data[0]), C.size_t(h.bufferSize))
|
|
|
|
if err := C.waveOutWrite(waveOut, &h.waveHdr, C.sizeOfWavehdr); err != C.MMSYSERR_NOERROR {
|
|
|
|
panic(fmt.Sprintf("audio: waveOutWriter error %d", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const numHeader = 8
|
2016-03-09 19:27:51 +01:00
|
|
|
|
2016-03-04 19:27:06 +01:00
|
|
|
var sem = make(chan struct{}, numHeader)
|
|
|
|
|
|
|
|
//export releaseSemaphore
|
|
|
|
func releaseSemaphore() {
|
|
|
|
<-sem
|
|
|
|
}
|
|
|
|
|
2016-04-03 19:42:37 +02:00
|
|
|
type player struct {
|
|
|
|
src io.Reader
|
|
|
|
out C.HWAVEOUT
|
|
|
|
i int
|
|
|
|
buffer []byte
|
|
|
|
headers []header
|
|
|
|
}
|
|
|
|
|
|
|
|
const bufferSize = 1024
|
|
|
|
|
2016-04-02 19:43:39 +02:00
|
|
|
func startPlaying(src io.Reader, sampleRate int) error {
|
2016-03-12 19:33:02 +01:00
|
|
|
const numBlockAlign = channelNum * bitsPerSample / 8
|
2016-03-04 19:27:06 +01:00
|
|
|
f := C.WAVEFORMATEX{
|
|
|
|
wFormatTag: C.WAVE_FORMAT_PCM,
|
2016-03-12 19:33:02 +01:00
|
|
|
nChannels: channelNum,
|
2016-03-04 19:27:06 +01:00
|
|
|
nSamplesPerSec: C.DWORD(sampleRate),
|
|
|
|
nAvgBytesPerSec: C.DWORD(sampleRate) * numBlockAlign,
|
|
|
|
wBitsPerSample: bitsPerSample,
|
|
|
|
nBlockAlign: numBlockAlign,
|
|
|
|
}
|
|
|
|
var w C.HWAVEOUT
|
|
|
|
if err := C.waveOutOpen2(&w, &f); err != C.MMSYSERR_NOERROR {
|
2016-04-02 19:43:39 +02:00
|
|
|
return fmt.Errorf("audio: waveOutOpen error: %d", err)
|
2016-03-04 19:27:06 +01:00
|
|
|
}
|
2016-04-03 19:42:37 +02:00
|
|
|
p := &player{
|
|
|
|
src: src,
|
|
|
|
out: w,
|
|
|
|
buffer: []byte{},
|
|
|
|
headers: make([]header, numHeader),
|
|
|
|
}
|
|
|
|
for i := 0; i < numHeader; i++ {
|
|
|
|
p.headers[i] = newHeader(w, bufferSize)
|
|
|
|
}
|
2016-03-04 19:27:06 +01:00
|
|
|
go func() {
|
2016-04-03 19:42:37 +02:00
|
|
|
defer p.close()
|
2016-03-04 19:27:06 +01:00
|
|
|
for {
|
2016-04-03 19:42:37 +02:00
|
|
|
err := p.proceed()
|
2016-03-04 19:27:06 +01:00
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
// TODO: Propagate this error?
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-03-13 17:13:11 +01:00
|
|
|
time.Sleep(1 * time.Millisecond)
|
2016-03-04 19:27:06 +01:00
|
|
|
}
|
|
|
|
}()
|
2016-04-02 19:43:39 +02:00
|
|
|
return nil
|
2015-06-20 16:39:56 +02:00
|
|
|
}
|
2016-04-03 19:42:37 +02:00
|
|
|
|
|
|
|
func (p *player) proceed() error {
|
|
|
|
b := make([]byte, bufferSize)
|
|
|
|
n, err := p.src.Read(b)
|
|
|
|
if 0 < n {
|
|
|
|
p.buffer = append(p.buffer, b[:n]...)
|
|
|
|
for bufferSize <= len(p.buffer) {
|
|
|
|
sem <- struct{}{}
|
|
|
|
p.headers[p.i].Write(p.out, p.buffer[:bufferSize])
|
|
|
|
p.buffer = p.buffer[bufferSize:]
|
|
|
|
p.i++
|
|
|
|
p.i %= len(p.headers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *player) close() {
|
|
|
|
// TODO: Implement this
|
|
|
|
}
|