Tonejs / Tone.js

A Web Audio framework for making interactive music in the browser.
https://tonejs.github.io
MIT License
13.51k stars 983 forks source link

Some possible compatibility issues with ios13.x #1069

Closed liJie-wk closed 2 years ago

liJie-wk commented 2 years ago

Hello authors, Tone .js is amazing! I successfully used it to make a drum machine and it worked very well. But I found that:

  1. It plays midi in a loop on iOS13.4 or 13.6, and a strange rhythm will appear after 4 to 5 minutes, but it does not occur on ios14.x and ios15.x, do you have any good solutions? Here's part of my code

    this.mainVolume = new Tone.Volume(0).toDestination();
    this.synth = new Tone.Sampler({
        urls: {},
        release: 1,
    }).connect(this.mainVolume);
    
    loadItems.forEach(item => {
        this.synth.add(item.name, this.pianoSamples.get(item.name))
    })
    this.synth.sync();
    
    await Tone.loaded()
    
    Tone.Transport.bpm.value = currentMidi.header.tempos[0].bpm
    Tone.Transport.loop = true
    Tone.Transport.loopEnd = currentMidi.duration + 0.002
    currentMidi.tracks[0].notes.forEach((note, index) => {
        this.synth.triggerAttackRelease(
            note.name,
            note.duration,
            note.time,
            note.velocity
        );
    });
    
    this.startOrPause(true)
  2. The same ios13.4 or 13.6, every time I switch midi and soundfont, I will first distribute and dispose, and then recreate. I found that frequent switching will cause memory leaks, which do not occur on iOS14.x and ios15.x, for which I improved the code to reduce the number of destroy creations, but I know that there is still a possibility of memory leaks

tambien commented 2 years ago

Unfortunately Tone.js does not attempt to support browsers that are more than 2 versions old. Browser compatibility, especially how it interacts with the Web Audio API is too complex to try and support every version of every browser. I hope you understand

liJie-wk commented 2 years ago

Unfortunately Tone.js does not attempt to support browsers that are more than 2 versions old. Browser compatibility, especially how it interacts with the Web Audio API is too complex to try and support every version of every browser. I hope you understand

Okay, thanks for the answer

chrisguttandin commented 2 years ago

Hi @liJie-wk, I do remember that the test suite of standardized-audio-context used to crash on older versions of Safari. The test suite consists of about 10000 tests which cause a lot of memory pressure. The crash implied that there was probably a memory leak somewhere. I never found out if the memory leak was in the codebase of standardized-audio-context or in Safari itself. The crashes didn't happen in other browsers but since Safari had and has by far the worst implementation of the Web Audio API it also was executing a lot of workarounds that didn't run in any other browser. So it's also possible that any of these workarounds caused the crash.

I think one thing you could try is to close() the existing AudioContext from time to time to replace it with a new one. Maybe this helps to trigger the garbage collection. It's just a guess. I'm not sure if it's actually going to help.

As Yotam already said it's all very complex.

liJie-wk commented 2 years ago

Hi @liJie-wk, I do remember that the test suite of standardized-audio-context used to crash on older versions of Safari. The test suite consists of about 10000 tests which cause a lot of memory pressure. The crash implied that there was probably a memory leak somewhere. I never found out if the memory leak was in the codebase of standardized-audio-context or in Safari itself. The crashes didn't happen in other browsers but since Safari had and has by far the worst implementation of the Web Audio API it also was executing a lot of workarounds that didn't run in any other browser. So it's also possible that any of these workarounds caused the crash.

I think one thing you could try is to close() the existing AudioContext from time to time to replace it with a new one. Maybe this helps to trigger the garbage collection. It's just a guess. I'm not sure if it's actually going to help.

As Yotam already said it's all very complex.

Okay, I get it, thank you very much!