oto icon indicating copy to clipboard operation
oto copied to clipboard

question about example

Open gucio321 opened this issue 1 year ago • 8 comments

Hi there!

context

I'm working on a morse code generator app. I've based my code on oto's example (sine wave is exactly what I needed).

My issue

After implementing the code in my app I noticed 2 issues:

  • (*SinWave).Read sometimes panics with index out of range [:-3XXXX] or, sometimes sound was corrupted (longer and glitchy). I managed to fix that by adding sync.Mutex (For some reason Read was called twice by oto, idk why).
  • the other issue may be specific to my app, where it must not appear, and may be wanted here in example to keep it as simple as possible: when the sound starts playing and when it ends, you can hear a "tap". It is a hardware thing imo. It could be removed by the following:
                b := int16(math.Sin(2*math.Pi*float64(p)/length) * 0.3 * max)

                if p <= stuckReduction {
                        b = int16(float64(p) / (stuckReduction) * math.Sin(2*math.Pi*float64(p)/length) * 0.3 *
                }

stuckReduction is a constant, which for me is 300 but could be reducd probably to about 100/200.

Let me know if my notes are worth adding to the example :smile:.

gucio321 avatar Jul 21 '24 09:07 gucio321

Does the current example cause crash? Then, could you file an issue and send a PR? Thanks,

hajimehoshi avatar Jul 21 '24 10:07 hajimehoshi

Or, if the crash happens in your app, I need to see your code...

hajimehoshi avatar Jul 21 '24 11:07 hajimehoshi

Well, I didn't manage to reproduce this crash with an example yet, maybe it is related to my Seek implementation or something?

my code is here: https://github.com/gucio321/morse/blob/master/pkg/generator/sine_wave.go try to comment out m.Lock/Unlock calls, then run cmd/gnerator. sometimes sounds are too long, sometimes (rarely) it crashes

gucio321 avatar Jul 22 '24 08:07 gucio321

you can run this: while true; do go run .; done

and after some time you should get

panic: runtime error: slice bounds out of range [:-17280]

goroutine 6 [running]:
github.com/gucio32/morse/pkg/generator.(*SineWave).Read(0xc000096af0, {0xc0000b0000?, 0x482fa9?, 0x0?})
        /home/me/git/morse/pkg/generator/sine_wave.go:54 +0x4f7
github.com/ebitengine/oto/v3/internal/mux.(*playerImpl).read(0xc0000bc000?, {0xc0000b0000?, 0xc000096b50?, 0x481200?})
        /home/me/go/pkg/mod/github.com/ebitengine/oto/[email protected]/internal/mux/mux.go:268 +0xc8
github.com/ebitengine/oto/v3/internal/mux.(*playerImpl).readSourceToBuffer(0xc0000aa1b0)
        /home/me/go/pkg/mod/github.com/ebitengine/oto/[email protected]/internal/mux/mux.go:529 +0x145
github.com/ebitengine/oto/v3/internal/mux.(*Mux).loop(0xc00008a0c0)
        /home/me/go/pkg/mod/github.com/ebitengine/oto/[email protected]/internal/mux/mux.go:108 +0x205
created by github.com/ebitengine/oto/v3/internal/mux.New in goroutine 1
        /home/me/go/pkg/mod/github.com/ebitengine/oto/[email protected]/internal/mux/mux.go:69 +0xf8
exit status 2

gucio321 avatar Jul 22 '24 08:07 gucio321

Oto implementation also uses mutex so Seek should be thread safe. I'll take a look later, but I hope you could create a more minimal case to reproduce the issue. Thanks,

hajimehoshi avatar Jul 22 '24 09:07 hajimehoshi

You should return an error when p.pos < 0 or p.pos > p.length at Seek at least.

hajimehoshi avatar Jul 22 '24 09:07 hajimehoshi

I found an interessting thing: if you modify an example like this: https://github.com/gucio321/oto/commit/d969893e144a41939b2a9ed9714f74eb53071bb3 not every sound is played

gucio321 avatar Jul 22 '24 09:07 gucio321

OK, please report issues one by one with a minimized test case to reproduce it. Thanks,

hajimehoshi avatar Jul 22 '24 09:07 hajimehoshi