2015-01-24 06:50:41 +01: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.
|
|
|
|
|
|
|
|
// +build !js
|
|
|
|
|
|
|
|
package audio
|
|
|
|
|
2015-01-24 14:19:10 +01:00
|
|
|
import (
|
2015-01-24 17:51:51 +01:00
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
2015-01-24 14:19:10 +01:00
|
|
|
"github.com/timshannon/go-openal/openal"
|
2015-01-25 06:28:24 +01:00
|
|
|
"log"
|
2015-01-24 17:51:51 +01:00
|
|
|
"math"
|
2015-01-24 14:19:10 +01:00
|
|
|
"runtime"
|
2015-01-24 17:51:51 +01:00
|
|
|
"time"
|
2015-01-24 14:19:10 +01:00
|
|
|
)
|
|
|
|
|
2015-01-24 17:51:51 +01:00
|
|
|
func toBytes(l, r []float32) []byte {
|
|
|
|
if len(l) != len(r) {
|
|
|
|
panic("len(l) must equal to len(r)")
|
|
|
|
}
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
for i, _ := range l {
|
|
|
|
l := int16(l[i] * math.MaxInt16)
|
|
|
|
r := int16(r[i] * math.MaxInt16)
|
|
|
|
if err := binary.Write(b, binary.LittleEndian, []int16{l, r}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
type sample struct {
|
|
|
|
l []float32
|
|
|
|
r []float32
|
|
|
|
}
|
|
|
|
|
2015-01-24 06:50:41 +01:00
|
|
|
func initialize() {
|
2015-01-24 14:19:10 +01:00
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
|
|
|
|
device := openal.OpenDevice("")
|
|
|
|
context := device.CreateContext()
|
|
|
|
context.Activate()
|
|
|
|
|
2015-01-24 17:51:51 +01:00
|
|
|
if alErr := openal.GetError(); alErr != 0 {
|
2015-01-25 06:28:24 +01:00
|
|
|
log.Printf("OpenAL initialize error: %d", alErr)
|
|
|
|
close(ch)
|
|
|
|
return
|
2015-01-24 17:51:51 +01:00
|
|
|
}
|
2015-01-24 14:19:10 +01:00
|
|
|
|
2015-01-25 06:28:24 +01:00
|
|
|
audioEnabled = true
|
|
|
|
source := openal.NewSource()
|
2015-01-24 14:19:10 +01:00
|
|
|
close(ch)
|
2015-01-24 17:51:51 +01:00
|
|
|
|
|
|
|
emptyBytes := make([]byte, 4*bufferSize)
|
|
|
|
|
|
|
|
const bufferNum = 16
|
|
|
|
buffers := openal.NewBuffers(bufferNum)
|
|
|
|
for _, buffer := range buffers {
|
|
|
|
buffer.SetData(openal.FormatStereo16, emptyBytes, SampleRate)
|
|
|
|
source.QueueBuffer(buffer)
|
|
|
|
}
|
|
|
|
source.Play()
|
|
|
|
if alErr := openal.GetError(); alErr != 0 {
|
|
|
|
panic(fmt.Sprintf("OpenAL error: %d", alErr))
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
if source.State() != openal.Playing {
|
|
|
|
panic(fmt.Sprintf("invalid source state: %d (0x%[1]x)", source.State()))
|
|
|
|
}
|
|
|
|
processed := source.BuffersProcessed()
|
|
|
|
if processed == 0 {
|
|
|
|
time.Sleep(1)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
buffers := make([]openal.Buffer, processed)
|
|
|
|
source.UnqueueBuffers(buffers)
|
|
|
|
for _, buffer := range buffers {
|
|
|
|
l, r := loadChannelBuffers()
|
|
|
|
b := toBytes(l, r)
|
|
|
|
buffer.SetData(openal.FormatStereo16, b, SampleRate)
|
|
|
|
source.QueueBuffer(buffer)
|
|
|
|
nextInsertionPosition -= min(bufferSize, nextInsertionPosition)
|
|
|
|
}
|
|
|
|
}
|
2015-01-24 14:19:10 +01:00
|
|
|
}()
|
|
|
|
<-ch
|
2015-01-24 06:50:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func start() {
|
2015-01-24 17:51:51 +01:00
|
|
|
// Do nothing
|
2015-01-24 06:50:41 +01:00
|
|
|
}
|