audio/internal/readerderiver: Bug fix: Reduce noises on Android

Closes #1632
This commit is contained in:
Hajime Hoshi 2021-05-05 02:14:56 +09:00
parent 83c9015468
commit c954dfa8fb
2 changed files with 19 additions and 1 deletions

View File

@ -57,6 +57,11 @@ public:
std::lock_guard<std::mutex> lock(GetPlayersMutex()); std::lock_guard<std::mutex> lock(GetPlayersMutex());
GetPlayers().insert(this); GetPlayers().insert(this);
} }
// Fill zeros with 1/60[s] as the first part to avoid noises (#1632).
// 1/60[s] is an arbitrary duration and might need to be adjusted.
size_t mul = channel_num_ * bit_depth_in_bytes_;
size_t size = (sample_rate_ * channel_num_ * bit_depth_in_bytes_) / 60 / mul * mul;
buf_.resize(size);
} }
void SetVolume(double volume) { void SetVolume(double volume) {

View File

@ -93,17 +93,30 @@ func (p *player) Play() {
return return
} }
defer p.cond.Signal() defer p.cond.Signal()
var runLoop bool
if p.p == nil { if p.p == nil {
p.p = oboe.NewPlayer(p.context.sampleRate, p.context.channelNum, p.context.bitDepthInBytes, p.volume, func() { p.p = oboe.NewPlayer(p.context.sampleRate, p.context.channelNum, p.context.bitDepthInBytes, p.volume, func() {
p.cond.Signal() p.cond.Signal()
}) })
go p.loop() runLoop = true
// Fill the first part before playing to reduce noises (#1632).
buf := make([]byte, p.context.oneBufferSize())
n, err := p.src.Read(buf)
if err != nil && err != io.EOF {
p.setErrorImpl(err)
return
}
p.p.AppendBuffer(buf[:n])
} }
if err := p.p.Play(); err != nil { if err := p.p.Play(); err != nil {
p.setErrorImpl(err) p.setErrorImpl(err)
return return
} }
p.state = playerPlay p.state = playerPlay
if runLoop {
go p.loop()
}
} }
func (p *player) IsPlaying() bool { func (p *player) IsPlaying() bool {