const bass = require('../old/basswrapper').basswrapper;
let basslib = new bass();
var result2 = basslib.BASS_Init(-1, 44100, basslib.BASS_DEVICE_STEREO)
if (!result2) {
    console.log('error init sound card:' + basslib.BASS_ErrorGetCode())
}

let rootPath = require('app-root-path').path;
let path = require('path')

let song = path.join(rootPath, 'test.mp3');
let mixerFlags = basslib.BASS_SAMPLE_DEFAULT
let chanCount = 2;
let addChannelFlags = basslib.BASS_SAMPLE_DEFAULT
let monoOut = false

if (monoOut) {
    chanCount = 1;
    mixerFlags = basslib.BASS_SPEAKER_FRONTRIGHT
    if (chanCount == 1) {
        addChannelFlags = basslib.BASS_MIXER_DOWNMIX
    }
}
let masterMixer = basslib.BASS_Mixer_StreamCreate(44100, chanCount, mixerFlags);
basslib.BASS_ChannelPlay(masterMixer, 1)
//if you dont want to give sound output to soundcard set this
//basslib.BASS_ChannelSetAttribute(masterMixer,basslib.BASS_ChannelAttributes.BASS_ATTRIB_VOL,0)

//------------------------------------------------------------------
//add your playlist manager code here
//create a mixer for playlistManager
let mixer = basslib.BASS_Mixer_StreamCreate(44100, 2, basslib.BASS_STREAM_DECODE);
let ok2 = basslib.BASS_Mixer_StreamAddChannel(masterMixer, mixer, basslib.BASS_SAMPLE_DEFAULT);

let chan1 = basslib.BASS_StreamCreateFile(false, song, 0, 0, basslib.BASS_STREAM_DECODE | basslib.BASS_SAMPLE_FLOAT)
let ok1 = basslib.BASS_Mixer_StreamAddChannel(mixer, chan1, addChannelFlags);
//------------------------------------------------------------------


//lets try icecast

let enc_chan = basslib.BASS_Encode_Start(masterMixer, 'lame -r -V 2 -b 56 -', basslib.BASS_ENCODE_NOHEAD);
let result = basslib.BASS_Encode_SetNotify(enc_chan, function (handle, status, user) {
    console.log('encoder msg:', status)

    if (status == basslib.BASS_ENCODE_NOTIFY_CAST) {
        console.log('server connection is dead');
    } else if (status == basslib.BASS_ENCODE_NOTIFY_ENCODER) {
        console.log('encoder problem')
    }
});
let result1 = basslib.BASS_Encode_CastInit(enc_chan,
    'http://carrefour.smg.com.tr:8000/cantest',
    '1q2w3e',
    basslib.BASS_ENCODE_TYPE_MP3,
    'test stream',
    'http://your-server-ip',
    'pop',
    'this is my new icecast test',
    'header1\r\nheader2\r\n',
    44100,
    true //public
);


function getVolume(channel) {
    let ref = require('ref')
//set a float pointer
    let volume = ref.alloc('float');
//get the volume
    let result = basslib.BASS_ChannelGetAttribute(channel, basslib.BASS_ATTRIB_VOL, volume);
//now deref volume to get the value
    return ref.deref(volume)
}


//show duration and vumeter levels
let intervalID = setInterval(() => {
    let chan = chan1
    if (basslib.BASS_ChannelIsActive(chan) == basslib.BASS_ACTIVE_PLAYING) {

        let position = basslib.BASS_ChannelBytes2Seconds(chan, basslib.BASS_ChannelGetPosition(chan, 0));
        let duration = basslib.BASS_ChannelBytes2Seconds(chan, basslib.BASS_ChannelGetLength(chan, 0))
        var levels = basslib.BASS_ChannelGetLevel(masterMixer);
        let vumeterleft = basslib.BASS_ChannelGetLeftLevel(levels);
        let vumeterright = basslib.BASS_ChannelGetRightLevel(levels);
        console.log(position + ' / ' + duration + '(' + vumeterleft + '/' + vumeterright + ')')

    } else {
        clearInterval(intervalID)
        console.log('stopped')
    }
}, 500)
