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-04-08 17:54:18 +02:00
|
|
|
package driver
|
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))
|
|
|
|
import "C"
|
|
|
|
|
2016-03-04 18:25:51 +01:00
|
|
|
import (
|
2016-04-03 19:50:00 +02:00
|
|
|
"errors"
|
2016-03-04 19:27:06 +01:00
|
|
|
"fmt"
|
|
|
|
"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-05 05:17:23 +02:00
|
|
|
func newHeader(waveOut C.HWAVEOUT, bufferSize int) (*header, error) {
|
2016-05-27 12:43:03 +02:00
|
|
|
// NOTE: This is never freed so far, and we don't have to because newHeader is called a certain number of times.
|
2016-03-04 19:27:06 +01:00
|
|
|
buf := C.malloc(C.size_t(bufferSize))
|
2016-04-05 05:17:23 +02:00
|
|
|
h := &header{
|
2016-03-04 19:27:06 +01:00
|
|
|
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),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := C.waveOutPrepareHeader(waveOut, &h.waveHdr, C.sizeOfWavehdr); err != C.MMSYSERR_NOERROR {
|
2016-04-08 18:10:42 +02:00
|
|
|
return nil, fmt.Errorf("driver: waveOutPrepareHeader error: %d", err)
|
2016-03-04 19:27:06 +01:00
|
|
|
}
|
2016-04-03 19:50:00 +02:00
|
|
|
return h, nil
|
2016-03-04 19:27:06 +01:00
|
|
|
}
|
|
|
|
|
2016-04-03 19:50:00 +02:00
|
|
|
func (h *header) Write(waveOut C.HWAVEOUT, data []byte) error {
|
2016-03-04 19:27:06 +01:00
|
|
|
if len(data) != h.bufferSize {
|
2016-04-08 18:10:42 +02:00
|
|
|
return errors.New("driver: len(data) must equal to h.bufferSize")
|
2016-03-04 19:27:06 +01:00
|
|
|
}
|
|
|
|
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 {
|
2016-04-08 18:10:42 +02:00
|
|
|
return fmt.Errorf("driver: waveOutWriter error: %d", err)
|
2016-03-04 19:27:06 +01:00
|
|
|
}
|
2016-04-03 19:50:00 +02:00
|
|
|
return nil
|
2016-03-04 19:27:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const numHeader = 8
|
2016-03-09 19:27:51 +01:00
|
|
|
|
2016-04-08 17:54:18 +02:00
|
|
|
type Player struct {
|
2016-04-03 19:42:37 +02:00
|
|
|
out C.HWAVEOUT
|
|
|
|
buffer []byte
|
2016-04-05 05:17:23 +02:00
|
|
|
headers []*header
|
2016-04-03 19:42:37 +02:00
|
|
|
}
|
|
|
|
|
2016-04-14 18:58:27 +02:00
|
|
|
const bufferSize = 4096
|
2016-04-03 19:42:37 +02:00
|
|
|
|
2016-04-14 18:37:48 +02:00
|
|
|
func NewPlayer(sampleRate, channelNum, bytesPerSample int) (*Player, error) {
|
2016-04-08 17:58:50 +02:00
|
|
|
numBlockAlign := channelNum * bytesPerSample
|
2016-03-04 19:27:06 +01:00
|
|
|
f := C.WAVEFORMATEX{
|
|
|
|
wFormatTag: C.WAVE_FORMAT_PCM,
|
2016-04-08 17:58:50 +02:00
|
|
|
nChannels: C.WORD(channelNum),
|
2016-03-04 19:27:06 +01:00
|
|
|
nSamplesPerSec: C.DWORD(sampleRate),
|
2016-04-08 17:58:50 +02:00
|
|
|
nAvgBytesPerSec: C.DWORD(sampleRate * numBlockAlign),
|
|
|
|
wBitsPerSample: C.WORD(bytesPerSample * 8),
|
|
|
|
nBlockAlign: C.WORD(numBlockAlign),
|
2016-03-04 19:27:06 +01:00
|
|
|
}
|
|
|
|
var w C.HWAVEOUT
|
2016-04-14 19:07:35 +02:00
|
|
|
if err := C.waveOutOpen(&w, C.WAVE_MAPPER, &f, 0, 0, C.CALLBACK_NULL); err != C.MMSYSERR_NOERROR {
|
2016-04-08 18:10:42 +02:00
|
|
|
return nil, fmt.Errorf("driver: waveOutOpen error: %d", err)
|
2016-03-04 19:27:06 +01:00
|
|
|
}
|
2016-04-08 17:54:18 +02:00
|
|
|
p := &Player{
|
2016-04-03 19:42:37 +02:00
|
|
|
out: w,
|
|
|
|
buffer: []byte{},
|
2016-04-05 05:17:23 +02:00
|
|
|
headers: make([]*header, numHeader),
|
2016-04-03 19:42:37 +02:00
|
|
|
}
|
|
|
|
for i := 0; i < numHeader; i++ {
|
2016-04-03 19:50:00 +02:00
|
|
|
var err error
|
|
|
|
p.headers[i], err = newHeader(w, bufferSize)
|
|
|
|
if err != nil {
|
2016-04-04 16:42:44 +02:00
|
|
|
return nil, err
|
2016-04-03 19:50:00 +02:00
|
|
|
}
|
2016-04-03 19:42:37 +02:00
|
|
|
}
|
2016-04-04 16:42:44 +02:00
|
|
|
return p, nil
|
2015-06-20 16:39:56 +02:00
|
|
|
}
|
2016-04-03 19:42:37 +02:00
|
|
|
|
2016-04-14 18:37:48 +02:00
|
|
|
func (p *Player) Proceed(data []byte) error {
|
|
|
|
p.buffer = append(p.buffer, data...)
|
|
|
|
if bufferSize > len(p.buffer) {
|
|
|
|
return nil
|
2016-04-05 05:17:23 +02:00
|
|
|
}
|
2016-04-14 18:37:48 +02:00
|
|
|
headerToWrite := (*header)(nil)
|
|
|
|
for _, h := range p.headers {
|
|
|
|
// TODO: Need to check WHDR_DONE?
|
|
|
|
if h.waveHdr.dwFlags&C.WHDR_INQUEUE == 0 {
|
|
|
|
headerToWrite = h
|
|
|
|
break
|
2016-04-05 05:17:23 +02:00
|
|
|
}
|
2016-04-03 19:42:37 +02:00
|
|
|
}
|
2016-04-14 18:37:48 +02:00
|
|
|
if headerToWrite == nil {
|
2016-04-16 05:09:25 +02:00
|
|
|
// This can happen (#207)
|
|
|
|
return nil
|
2016-04-14 18:37:48 +02:00
|
|
|
}
|
|
|
|
if err := headerToWrite.Write(p.out, p.buffer[:bufferSize]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.buffer = p.buffer[bufferSize:]
|
2016-04-05 05:17:23 +02:00
|
|
|
return nil
|
2016-04-03 19:42:37 +02:00
|
|
|
}
|
|
|
|
|
2016-04-14 18:58:27 +02:00
|
|
|
func (p *Player) Close() error {
|
2016-04-03 19:42:37 +02:00
|
|
|
// TODO: Implement this
|
2016-04-14 18:58:27 +02:00
|
|
|
return nil
|
2016-04-03 19:42:37 +02:00
|
|
|
}
|