Get sound bytes

Hello :slight_smile:

I would like to try to do a visual analyser (with interaction) of the sound variation with Heap (not sure that it’s the best tool for that, but I’d like to learn Heap too^^), but I’m facing an error in javascript when I try to manipulate haxe.io.Bytes from the music loaded.

This is my code :
import hxd.res.Sound;

class Main extends hxd.App {
    var music: Sound = null;
    var channel: hxd.snd.Channel;
    var bytes: haxe.io.Bytes;

    override function init() {
        if (hxd.res.Sound.supportedFormat(Mp3)) {
            this.music = hxd.Res.my_sound;
        }

        if (this.music != null) {
            this.music.getData().load(function() {
                this.bytes = this.music.entry.getBytes();
                this.channel = this.music.play(true, 0.2);
            });
        }
    }

    override function update(dt : Float) {
        if (this.channel != null) {
            // Crash
            trace(this.music.entry.getBytes().sub(100, 200));
        }
    }

    static function main() {
        hxd.Res.initEmbed();
        new Main();
    }
}

This code breaks in javascript with the following error : Uncaught TypeError: Cannot perform ArrayBuffer.prototype.slice on a detached ArrayBuffer (see update function, it crashes in the init function too). I get similar error with getDouble, getFloat, getUInt16, etc… Only get() and getData() seems to work, but get() always returns null, for all positions.

So, my question is, how to get the loaded music’s bytes (if it’s possible) ? Or is there a tool that can integrate with Heap that’s needed to do this kind of job ?

Thank you,
Peekmo

Why are you trying to get bytes by using this.music.entry.getBytes() in update() method?
You already have bytes of music in game init() method.
Btw, this code works, but i’m not sure if it’s what you need:

package;

class Game extends hxd.App 
{
    var music: hxd.res.Sound = null;
    var channel: hxd.snd.Channel;
    var bytes: haxe.io.Bytes;

    override function init() 
    {
        if (hxd.res.Sound.supportedFormat(Mp3)) 
        {
            var musicRes = hxd.Res.load("Music.mp3");
            this.bytes = musicRes.entry.getBytes();
            music = musicRes.toSound();

            this.channel = this.music.play(true, 0.2);
        }
    }

    override function update(dt : Float) 
    {
        trace(this.bytes.sub(100, 200));
    }

    static function main() 
    {
        hxd.Res.initEmbed();
        new Game();
    }
}
1 Like

It was in the update method because it was my latest test, I was a little bit lost…
Thank you, in fact it works using hxd.Res.load() instead of the method I used to load the sound.