2016-05-24 19:02:58 +02:00
|
|
|
// Copyright 2016 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.
|
|
|
|
|
|
|
|
package driver
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
#cgo LDFLAGS: -llog
|
|
|
|
|
|
|
|
#include <android/log.h>
|
|
|
|
#include <jni.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
// __android_log_print(ANDROID_LOG_ERROR, "NativeCode", "foo", "bar");
|
|
|
|
|
2016-05-27 19:54:29 +02:00
|
|
|
static char* initAudioTrack(uintptr_t java_vm, uintptr_t jni_env,
|
2016-05-25 18:46:26 +02:00
|
|
|
int sampleRate, int channelNum, int bytesPerSample, jobject* audioTrack, int* bufferSize) {
|
|
|
|
*bufferSize = 0;
|
2016-05-24 19:02:58 +02:00
|
|
|
JavaVM* vm = (JavaVM*)java_vm;
|
|
|
|
JNIEnv* env = (JNIEnv*)jni_env;
|
|
|
|
|
|
|
|
const jclass android_media_AudioFormat =
|
|
|
|
(*env)->FindClass(env, "android/media/AudioFormat");
|
|
|
|
const jclass android_media_AudioManager =
|
|
|
|
(*env)->FindClass(env, "android/media/AudioManager");
|
|
|
|
const jclass android_media_AudioTrack =
|
|
|
|
(*env)->FindClass(env, "android/media/AudioTrack");
|
|
|
|
|
|
|
|
const jint android_media_AudioManager_STREAM_MUSIC =
|
|
|
|
(*env)->GetStaticIntField(
|
|
|
|
env, android_media_AudioManager,
|
|
|
|
(*env)->GetStaticFieldID(env, android_media_AudioManager, "STREAM_MUSIC", "I"));
|
|
|
|
const jint android_media_AudioTrack_MODE_STREAM =
|
|
|
|
(*env)->GetStaticIntField(
|
|
|
|
env, android_media_AudioTrack,
|
|
|
|
(*env)->GetStaticFieldID(env, android_media_AudioTrack, "MODE_STREAM", "I"));
|
|
|
|
const jint android_media_AudioFormat_CHANNEL_OUT_MONO =
|
|
|
|
(*env)->GetStaticIntField(
|
|
|
|
env, android_media_AudioFormat,
|
|
|
|
(*env)->GetStaticFieldID(env, android_media_AudioFormat, "CHANNEL_OUT_MONO", "I"));
|
|
|
|
const jint android_media_AudioFormat_CHANNEL_OUT_STEREO =
|
|
|
|
(*env)->GetStaticIntField(
|
|
|
|
env, android_media_AudioFormat,
|
|
|
|
(*env)->GetStaticFieldID(env, android_media_AudioFormat, "CHANNEL_OUT_STEREO", "I"));
|
|
|
|
const jint android_media_AudioFormat_ENCODING_PCM_8BIT =
|
|
|
|
(*env)->GetStaticIntField(
|
|
|
|
env, android_media_AudioFormat,
|
|
|
|
(*env)->GetStaticFieldID(env, android_media_AudioFormat, "ENCODING_PCM_8BIT", "I"));
|
|
|
|
const jint android_media_AudioFormat_ENCODING_PCM_16BIT =
|
|
|
|
(*env)->GetStaticIntField(
|
|
|
|
env, android_media_AudioFormat,
|
|
|
|
(*env)->GetStaticFieldID(env, android_media_AudioFormat, "ENCODING_PCM_16BIT", "I"));
|
|
|
|
|
|
|
|
jint channel = android_media_AudioFormat_CHANNEL_OUT_MONO;
|
|
|
|
switch (channelNum) {
|
|
|
|
case 1:
|
|
|
|
channel = android_media_AudioFormat_CHANNEL_OUT_MONO;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
channel = android_media_AudioFormat_CHANNEL_OUT_STEREO;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return "invalid channel";
|
|
|
|
}
|
|
|
|
|
|
|
|
jint encoding = android_media_AudioFormat_ENCODING_PCM_8BIT;
|
|
|
|
switch (bytesPerSample) {
|
|
|
|
case 1:
|
2016-05-25 18:46:26 +02:00
|
|
|
encoding = android_media_AudioFormat_ENCODING_PCM_8BIT;
|
2016-05-24 19:02:58 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
2016-05-25 18:46:26 +02:00
|
|
|
encoding = android_media_AudioFormat_ENCODING_PCM_16BIT;
|
2016-05-24 19:02:58 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return "invalid bytesPerSample";
|
|
|
|
}
|
|
|
|
|
2016-05-25 18:46:26 +02:00
|
|
|
*bufferSize =
|
2016-05-24 19:02:58 +02:00
|
|
|
(*env)->CallStaticIntMethod(
|
|
|
|
env, android_media_AudioTrack,
|
|
|
|
(*env)->GetStaticMethodID(env, android_media_AudioTrack, "getMinBufferSize", "(III)I"),
|
|
|
|
sampleRate, channel, encoding);
|
|
|
|
|
|
|
|
const jobject tmpAudioTrack =
|
|
|
|
(*env)->NewObject(
|
|
|
|
env, android_media_AudioTrack,
|
|
|
|
(*env)->GetMethodID(env, android_media_AudioTrack, "<init>", "(IIIIII)V"),
|
|
|
|
android_media_AudioManager_STREAM_MUSIC,
|
2016-05-25 18:46:26 +02:00
|
|
|
sampleRate, channel, encoding, *bufferSize,
|
2016-05-24 19:02:58 +02:00
|
|
|
android_media_AudioTrack_MODE_STREAM);
|
|
|
|
// Note that *audioTrack will never be released.
|
|
|
|
*audioTrack = (*env)->NewGlobalRef(env, tmpAudioTrack);
|
|
|
|
|
|
|
|
(*env)->CallVoidMethod(
|
|
|
|
env, *audioTrack,
|
|
|
|
(*env)->GetMethodID(env, android_media_AudioTrack, "play", "()V"));
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-05-27 19:54:29 +02:00
|
|
|
static char* writeToAudioTrack(uintptr_t java_vm, uintptr_t jni_env,
|
2016-05-25 18:46:26 +02:00
|
|
|
jobject audioTrack, int bytesPerSample, void* data, int length) {
|
2016-05-24 19:02:58 +02:00
|
|
|
JavaVM* vm = (JavaVM*)java_vm;
|
|
|
|
JNIEnv* env = (JNIEnv*)jni_env;
|
|
|
|
|
|
|
|
const jclass android_media_AudioTrack =
|
|
|
|
(*env)->FindClass(env, "android/media/AudioTrack");
|
|
|
|
|
2016-05-25 18:46:26 +02:00
|
|
|
jbyteArray arrInBytes;
|
|
|
|
jshortArray arrInShorts;
|
|
|
|
switch (bytesPerSample) {
|
|
|
|
case 1:
|
|
|
|
arrInBytes = (*env)->NewByteArray(env, length);
|
|
|
|
(*env)->SetByteArrayRegion(env, arrInBytes, 0, length, data);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
arrInShorts = (*env)->NewShortArray(env, length);
|
|
|
|
(*env)->SetShortArrayRegion(env, arrInShorts, 0, length, data);
|
|
|
|
break;
|
|
|
|
}
|
2016-05-28 21:40:30 +02:00
|
|
|
|
|
|
|
jint result;
|
|
|
|
switch (bytesPerSample) {
|
|
|
|
case 1:
|
|
|
|
result =
|
|
|
|
(*env)->CallIntMethod(
|
|
|
|
env, audioTrack,
|
2016-05-31 15:04:14 +02:00
|
|
|
(*env)->GetMethodID(env, android_media_AudioTrack, "write", "([BII)I"),
|
|
|
|
arrInBytes, 0, length);
|
2016-05-28 21:40:30 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
result =
|
|
|
|
(*env)->CallIntMethod(
|
|
|
|
env, audioTrack,
|
2016-05-31 15:04:14 +02:00
|
|
|
(*env)->GetMethodID(env, android_media_AudioTrack, "write", "([SII)I"),
|
|
|
|
arrInShorts, 0, length);
|
2016-05-28 21:40:30 +02:00
|
|
|
break;
|
2016-05-25 18:46:26 +02:00
|
|
|
}
|
2016-05-24 19:02:58 +02:00
|
|
|
|
|
|
|
// TODO: Check the result.
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"unsafe"
|
2016-05-27 19:40:13 +02:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/jni"
|
2016-05-24 19:02:58 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Player struct {
|
|
|
|
sampleRate int
|
|
|
|
channelNum int
|
|
|
|
bytesPerSample int
|
|
|
|
audioTrack C.jobject
|
2016-05-25 18:46:26 +02:00
|
|
|
buffer []byte
|
|
|
|
bufferSize int
|
|
|
|
chErr chan error
|
2016-05-25 19:41:00 +02:00
|
|
|
chBuffer chan []byte
|
2016-05-24 19:02:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewPlayer(sampleRate, channelNum, bytesPerSample int) (*Player, error) {
|
|
|
|
p := &Player{
|
|
|
|
sampleRate: sampleRate,
|
|
|
|
channelNum: channelNum,
|
|
|
|
bytesPerSample: bytesPerSample,
|
2016-05-25 18:46:26 +02:00
|
|
|
buffer: []byte{},
|
|
|
|
chErr: make(chan error),
|
2016-05-31 03:17:22 +02:00
|
|
|
chBuffer: make(chan []byte, 8),
|
2016-05-24 19:02:58 +02:00
|
|
|
}
|
2016-05-27 19:40:13 +02:00
|
|
|
if err := jni.RunOnJVM(func(vm, env, ctx uintptr) error {
|
2016-05-24 19:02:58 +02:00
|
|
|
audioTrack := C.jobject(nil)
|
2016-05-25 18:46:26 +02:00
|
|
|
bufferSize := C.int(0)
|
2016-05-27 19:54:29 +02:00
|
|
|
if msg := C.initAudioTrack(C.uintptr_t(vm), C.uintptr_t(env),
|
2016-05-24 19:02:58 +02:00
|
|
|
C.int(sampleRate), C.int(channelNum), C.int(bytesPerSample),
|
2016-05-25 18:46:26 +02:00
|
|
|
&audioTrack, &bufferSize); msg != nil {
|
2016-05-27 19:54:29 +02:00
|
|
|
return errors.New("driver: " + C.GoString(msg))
|
2016-05-24 19:02:58 +02:00
|
|
|
}
|
|
|
|
p.audioTrack = audioTrack
|
2016-05-25 18:46:26 +02:00
|
|
|
p.bufferSize = int(bufferSize)
|
2016-05-24 19:02:58 +02:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-05-25 20:15:51 +02:00
|
|
|
go p.loop()
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) loop() {
|
|
|
|
for bufInBytes := range p.chBuffer {
|
|
|
|
var bufInShorts []int16
|
|
|
|
if p.bytesPerSample == 2 {
|
|
|
|
bufInShorts = make([]int16, len(bufInBytes)/2)
|
|
|
|
for i := 0; i < len(bufInShorts); i++ {
|
|
|
|
bufInShorts[i] = int16(bufInBytes[2*i]) | (int16(bufInBytes[2*i+1]) << 8)
|
2016-05-25 19:41:00 +02:00
|
|
|
}
|
2016-05-25 20:15:51 +02:00
|
|
|
}
|
2016-05-25 19:41:00 +02:00
|
|
|
|
2016-05-27 19:40:13 +02:00
|
|
|
if err := jni.RunOnJVM(func(vm, env, ctx uintptr) error {
|
2016-05-25 20:15:51 +02:00
|
|
|
msg := (*C.char)(nil)
|
|
|
|
switch p.bytesPerSample {
|
|
|
|
case 1:
|
2016-05-27 19:54:29 +02:00
|
|
|
msg = C.writeToAudioTrack(C.uintptr_t(vm), C.uintptr_t(env),
|
2016-05-25 20:15:51 +02:00
|
|
|
p.audioTrack, C.int(p.bytesPerSample),
|
|
|
|
unsafe.Pointer(&bufInBytes[0]), C.int(len(bufInBytes)))
|
|
|
|
case 2:
|
2016-05-27 19:54:29 +02:00
|
|
|
msg = C.writeToAudioTrack(C.uintptr_t(vm), C.uintptr_t(env),
|
2016-05-25 20:15:51 +02:00
|
|
|
p.audioTrack, C.int(p.bytesPerSample),
|
|
|
|
unsafe.Pointer(&bufInShorts[0]), C.int(len(bufInShorts)))
|
2016-05-25 19:41:00 +02:00
|
|
|
}
|
2016-05-25 20:15:51 +02:00
|
|
|
if msg != nil {
|
|
|
|
return errors.New(C.GoString(msg))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
p.chErr <- err
|
|
|
|
return
|
2016-05-25 19:41:00 +02:00
|
|
|
}
|
2016-05-25 20:15:51 +02:00
|
|
|
}
|
2016-05-24 19:02:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) Proceed(data []byte) error {
|
2016-05-25 18:46:26 +02:00
|
|
|
p.buffer = append(p.buffer, data...)
|
|
|
|
if len(p.buffer) < p.bufferSize {
|
|
|
|
return nil
|
|
|
|
}
|
2016-05-25 19:41:00 +02:00
|
|
|
buf := p.buffer[:p.bufferSize]
|
2016-05-25 20:13:56 +02:00
|
|
|
select {
|
|
|
|
case p.chBuffer <- buf:
|
|
|
|
case err := <-p.chErr:
|
|
|
|
return err
|
|
|
|
}
|
2016-05-25 18:46:26 +02:00
|
|
|
p.buffer = p.buffer[p.bufferSize:]
|
2016-05-24 19:02:58 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|