audio: Refactoring: Add loop

This commit is contained in:
Hajime Hoshi 2016-05-26 03:15:51 +09:00
parent d6dee0815e
commit ca2f85f3d1

View File

@ -219,39 +219,41 @@ func NewPlayer(sampleRate, channelNum, bytesPerSample int) (*Player, error) {
}); err != nil { }); err != nil {
return nil, err return nil, err
} }
go func() { go p.loop()
for bufInBytes := range p.chBuffer { return p, nil
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)
}
}
if err := runOnJVM(func(vm, env, ctx uintptr) error { func (p *Player) loop() {
msg := (*C.char)(nil) for bufInBytes := range p.chBuffer {
switch p.bytesPerSample { var bufInShorts []int16
case 1: if p.bytesPerSample == 2 {
msg = C.writeToAudioTrack(C.uintptr_t(vm), C.uintptr_t(env), C.jobject(ctx), bufInShorts = make([]int16, len(bufInBytes)/2)
p.audioTrack, C.int(p.bytesPerSample), for i := 0; i < len(bufInShorts); i++ {
unsafe.Pointer(&bufInBytes[0]), C.int(len(bufInBytes))) bufInShorts[i] = int16(bufInBytes[2*i]) | (int16(bufInBytes[2*i+1]) << 8)
case 2:
msg = C.writeToAudioTrack(C.uintptr_t(vm), C.uintptr_t(env), C.jobject(ctx),
p.audioTrack, C.int(p.bytesPerSample),
unsafe.Pointer(&bufInShorts[0]), C.int(len(bufInShorts)))
}
if msg != nil {
return errors.New(C.GoString(msg))
}
return nil
}); err != nil {
p.chErr <- err
return
} }
} }
}()
return p, nil if err := runOnJVM(func(vm, env, ctx uintptr) error {
msg := (*C.char)(nil)
switch p.bytesPerSample {
case 1:
msg = C.writeToAudioTrack(C.uintptr_t(vm), C.uintptr_t(env), C.jobject(ctx),
p.audioTrack, C.int(p.bytesPerSample),
unsafe.Pointer(&bufInBytes[0]), C.int(len(bufInBytes)))
case 2:
msg = C.writeToAudioTrack(C.uintptr_t(vm), C.uintptr_t(env), C.jobject(ctx),
p.audioTrack, C.int(p.bytesPerSample),
unsafe.Pointer(&bufInShorts[0]), C.int(len(bufInShorts)))
}
if msg != nil {
return errors.New(C.GoString(msg))
}
return nil
}); err != nil {
p.chErr <- err
return
}
}
} }
func (p *Player) Proceed(data []byte) error { func (p *Player) Proceed(data []byte) error {