jeremyfa / linc_soloud

Soloud bindings for Haxe C++ target
MIT License
6 stars 1 forks source link

Trying to make a sound channel using the haxelib and I get weird compilation errors #2

Open SomeGuyWhoLovesCoding opened 6 months ago

SomeGuyWhoLovesCoding commented 6 months ago

Code:

package;

import soloud.AudioSource;
import soloud.Soloud;
import soloud.WavStream;

class SoundChannel
{
    @:unreflective private var engine:Soloud;
    @:unreflective private var sampleList:Map<String, WavStream>;
    @:unreflective private var handleList:Map<String, Handle>;

    public function new():Void
    {
        if (engine == null)
        {
            engine = Soloud.create();
            engine.init();
        }

        if (sampleList == null)
        {
            sampleList = new Map<String, WavStream>();
        }

        if (handleList == null)
        {
            handleList = new Map<String, Handle>();
        }
    }

    public function play(soundID:String, file:String):Void
    {
        if (engine == null)
            return;

        if (sampleList.exists(file))
            stop(file);

        sampleList.set(soundID, WavStream.create());

        var sample = sampleList.get(soundID);

        sample.load(soundID);
        handleList.set(soundID, engine.play(sample));
    }

    public function setPause(soundID:String, pause:Bool = false):Void
    {
        if (engine != null && handleList.exists(soundID))
            engine.setPause(handleList.get(soundID), pause);
    }

    public function getPause(soundID:String):Bool
    {
        if (engine != null && handleList.exists(soundID))
            return engine.getPause(handleList.get(soundID));
        return false;
    }

    public function setPlaybackSpeed(soundID:String, speed:Float = 1.0):Void
    {
        if (engine != null && handleList.exists(soundID))
            engine.setRelativePlaySpeed(handleList.get(soundID), speed);
    }

    public function setVolume(soundID:String, volume:Float = 1.0):Void
    {
        if (engine != null && handleList.exists(soundID))
            engine.setVolume(handleList.get(soundID), volume);
    }

    public function stop(soundID:String):Void
    {
        if (engine != null && handleList.exists(soundID))
        {
            engine.stop(handleList.get(soundID));
            handleList.remove(soundID);
        }

        if (sampleList.exists(soundID))
        {
            sampleList.get(soundID).destroy();
            sampleList.remove(soundID);
        }
    }

    public function getLengthOf(soundID:String):Float
    {
        if (sampleList.exists(soundID))
            return sampleList.get(soundID).getLength();
        return 0.0;
    }

    public function getTimeOf(soundID:String):Float
    {
        if (engine != null && handleList.exists(soundID))
            return engine.getStreamTime(handleList.get(soundID));
        return 0.0;
    }

    public function setPauseAll(pause:Bool = false):Void
    {
        if (engine != null)
            engine.setPauseAll(pause);
    }

    public function stopAll():Void
    {
        if (engine != null)
            engine.stopAll();
    }
}

Error:


./src/SoundChannel.cpp(70): error C2440: 'type cast': cannot convert from 'Dynamic' to 'SoLoud::WavStream *'
./src/SoundChannel.cpp(70): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
./src/SoundChannel.cpp(174): error C2440: 'type cast': cannot convert from 'Dynamic' to 'SoLoud::WavStream *'
./src/SoundChannel.cpp(174): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
./src/SoundChannel.cpp(186): error C2440: 'type cast': cannot convert from 'Dynamic' to 'SoLoud::WavStream *'
./src/SoundChannel.cpp(186): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called```
SomeGuyWhoLovesCoding commented 6 months ago

This is playstate:

package;

import flixel.*;

class PlayState extends FlxState
{
    public var channel:SoundChannel;

    override public function create()
    {
        FlxG.stage.window.frameRate = 57.59999999999998;

        super.create();

        channel = new SoundChannel();
    }

    override public function update(elapsed:Float)
    {
        if (FlxG.keys.justPressed.ENTER)
        {
            channel.play("music", "assets/ogg.ogg");
        }

        if (FlxG.keys.justPressed.SPACE)
        {
            channel.setPause("music", !channel.getPause("music"));
        }

        if (FlxG.keys.justPressed.ESCAPE)
        {
            channel.stop("music");
        }

        trace(formatTime(channel.getTimeOf("music"), true, true), formatTime(channel.getLengthOf("music"), true, true));

        super.update(elapsed);
    }

    /**
    * Format seconds as minutes with a colon, an optionally with milliseconds too.
    *
    * @param    ms      The number of seconds (for example, time remaining, time spent, etc).
    * @param    EnglishStyle        Whether to show a ":" instead of a ";".
    * @param    showMS      Whether to show a "." after seconds aswell.
    * @return   A nicely formatted String, like "1:03.45".
    */
    inline public function formatTime(ms:Float, englishStyle:Bool, showMS:Bool):String
    {
        // Rewritten code??? Moment when I use ChatGPT:
        var milliseconds:Int = Std.int(ms * 0.1) % 100;
        var seconds:Int = Std.int(ms * 0.001);
        var hours:Int = Std.int(seconds / 3600);
        seconds %= 3600;
        var minutes:Int = Std.int(seconds / 60);
        seconds %= 60;

        var t:String = ';';

        if (englishStyle)
            t = ':';

        var c:String = ',';

        if (englishStyle)
            c = '.';

        var time:String = '';
        if (!Math.isNaN(ms)) {
            if (hours > 0) time += '$hours$t';
            time += '${minutes < 10 && hours > 0 ? '0' : ''}$minutes$t';
            if (seconds < 10) time += '0';
            time += seconds;
        } else
            time = 'NaN';

        if (showMS) {
            if (milliseconds < 10) 
                time += '${c}0$milliseconds';
            else
                time += '${c}$milliseconds';
        }

        return time;
    }
}