Beispiel #1
0
    public void ReadStream(Stream stream)
    {
        byte[] bytes = new byte[stream.Length];
        stream.Read(bytes, 0, bytes.Length);

        string name   = null;
        int    slot   = SunVoxUtils.OpenUnusedSlot();
        int    result = SunVox.sv_load_from_memory(slot, bytes, bytes.Length);

        if (result == 0)
        {
            name = System.Runtime.InteropServices.Marshal.PtrToStringAuto(SunVox.sv_get_song_name(slot));
        }
        SunVoxUtils.CloseSlot(slot);
        if (name != null)
        {
            name = name.Trim();
        }
        if (name != null && name.Length > 32)
        {
            name = name.Substring(0, 32);
        }
        if (name == null || name == "")
        {
            name = "imported";
        }

        data = new EmbeddedData(name, bytes, EmbeddedDataType.SunVox);
    }
Beispiel #2
0
    private void Start()
    {
        log("-Press Space for toggle music-\n");

        try {
            int ver = SunVox.sv_init("0", 44100, 2, 0);
            if (ver >= 0)
            {
                int major  = (ver >> 16) & 255;
                int minor1 = (ver >> 8) & 255;
                int minor2 = (ver) & 255;
                log(String.Format("SunVox lib version: {0}.{1}.{2}", major, minor1, minor2));

                SunVox.sv_open_slot(0);

                log("Loading SunVox song from memory...");
                loadBinaryAsset(getDataPath("test.sunvox"), onBinaryFileLoaded);
            }
            else
            {
                log("sv_init() error " + ver);
            }
        } catch (Exception e) {
            log("Exception: " + e);
        }
    }
Beispiel #3
0
    private IEnumerator ModuleCoroutine()
    {
        //Create Generator module:
        SunVox.sv_lock_slot(0);
        int mod_num = SunVox.sv_new_module(0, "Generator", "Generator", 0, 0, 0);

        SunVox.sv_unlock_slot(0);
        if (mod_num >= 0)
        {
            log("New module created: " + mod_num);
            //Connect the new module to the Main Output:
            SunVox.sv_lock_slot(0);
            SunVox.sv_connect_module(0, mod_num, 0);
            SunVox.sv_unlock_slot(0);
            //Send Note ON:
            log("Note ON");
            SunVox.sv_send_event(0, 0, 64, 128, mod_num + 1, 0, 0);
            yield return(new WaitForSeconds(1));

            //Send Note OFF:
            log("Note OFF");
            SunVox.sv_send_event(0, 0, 128, 128, mod_num + 1, 0, 0);
            yield return(new WaitForSeconds(1));
        }
        else
        {
            log("Can't create the new module");
        }

        //Load module and play it:
        log("Load module and play it");
        loadBinaryAsset(getDataPath("organ.sunsynth"), onBinaryFileLoaded);

        yield return(new WaitForSeconds(1));
    }
Beispiel #4
0
    private IEnumerator SamplerCoroutine()
    {
        //Create Sampler module:
        SunVox.sv_lock_slot(0);
        int mod_num = SunVox.sv_new_module(0, "Sampler", "Sampler", 0, 0, 0);

        SunVox.sv_unlock_slot(0);
        if (mod_num >= 0)
        {
            log("New module created: " + mod_num);
            //Connect the new module to the Main Output:
            SunVox.sv_lock_slot(0);
            SunVox.sv_connect_module(0, mod_num, 0);
            SunVox.sv_unlock_slot(0);
            //Load a sample:
            var path = "Assets/StreamingAssets/flute.xi"; // This path is correct only for standalone
            SunVox.sv_sampler_load(0, mod_num, path, -1);
            //Send Note ON:
            log("Note ON");
            SunVox.sv_send_event(0, 0, 64, 128, mod_num + 1, 0, 0);
            yield return(new WaitForSeconds(1));

            //Send Note OFF:
            log("Note OFF");
            SunVox.sv_send_event(0, 0, 128, 128, mod_num + 1, 0, 0);
            yield return(new WaitForSeconds(1));
        }
        else
        {
            log("Can't create the new module");
        }
    }
Beispiel #5
0
    public void PlayMusic(string filename, bool repeat = false, byte volume = Byte.MaxValue)
    {
        if (Disabled)
        {
            return;
        }

//		var path = $"Assets/StreamingAssets/Tracker/{filename}";
        var path = GetDataPath("Tracker/" + filename);

        Logger.Log($"Loading track {filename} from {path}", Category.SunVox);
        int loadResult = SunVox.sv_load((int)Slot.Music, path);

        if (loadResult == 0)
        {
            SunVox.sv_stop((int)Slot.Music);
            SunVox.sv_set_autostop((int)Slot.Music, repeat ? 0 : 1);
            SunVox.sv_volume((int)Slot.Music, volume);
            SunVox.sv_play_from_beginning((int)Slot.Music);
        }
        else
        {
            Logger.LogWarning($"Music load error: {path}", Category.SunVox);
        }
    }
Beispiel #6
0
    private IEnumerator PlayNote(int module)
    {
        SunVox.sv_send_event((int)Slot.FX, 0, Random.Range(48, 72), 128, module + 1, 0, 0);
        yield return(WaitFor.Seconds(0.1f));

        SunVox.sv_send_event((int)Slot.FX, 0, 128, 128, module + 1, 0, 0);
    }
Beispiel #7
0
    public void Init()
    {
        if (songData.bytes.Length == 0)
        {
            return;
        }
        slot = SunVoxUtils.OpenUnusedSlot();
        if (slot < 0)
        {
            return;
        }
        int result = SunVox.sv_load_from_memory(slot, songData.bytes, songData.bytes.Length);

        if (result != 0)
        {
            throw new MapReadException("Error reading SunVox file");
        }
        //Debug.Log(System.Runtime.InteropServices.Marshal.PtrToStringAuto(SunVox.sv_get_song_name(0)));

        currentVolume = 0;
        UpdateSunVoxVolume();

        if (playMode == PlayMode.ONCE || playMode == PlayMode._1SHOT)
        {
            SunVox.sv_set_autostop(slot, 1);
        }
        else
        {
            SunVox.sv_set_autostop(slot, 0);
        }

        StartCoroutine(VolumeUpdateCoroutine()); // runs even while disabled
    }
Beispiel #8
0
 public override void BehaviorEnabled()
 {
     if (slot < 0)
     {
         return;
     }
     if (playMode != PlayMode.BKGND)
     {
         if (fadeIn == 0)
         {
             currentVolume = volume;
         }
         else
         {
             currentVolume = 0;
         }
     }
     fadingIn  = true;
     fadingOut = false;
     UpdateSunVoxVolume();
     if (playMode != PlayMode.BKGND)
     {
         SunVox.sv_play_from_beginning(slot);
     }
 }
Beispiel #9
0
    private IEnumerator SamplerCoroutine()
    {
        //Create Sampler module:
        SunVox.sv_lock_slot(0);
        int mod_num = SunVox.sv_new_module(0, "Sampler", "Sampler", 0, 0, 0);

        SunVox.sv_unlock_slot(0);
        if (mod_num >= 0)
        {
            log("New module created: " + mod_num);
            //Connect the new module to the Main Output:
            SunVox.sv_lock_slot(0);
            SunVox.sv_connect_module(0, mod_num, 0);
            SunVox.sv_unlock_slot(0);

            //Load a sample:
            log("Load sample and play it");
            loadBinaryAsset(getDataPath("flute.xi"), () => StartCoroutine(onBinaryFileLoadedCoroutine(mod_num)));

            yield return(new WaitForSeconds(1));
        }
        else
        {
            log("Can't create the new module");
        }
    }
Beispiel #10
0
    private void Init()
    {
        if (Initialized || Disabled)
        {
            return;
        }

        try
        {
            int ver = SunVox.sv_init("0", 11025, 2, 0);             //lo-fi 4ever
            if (ver >= 0)
            {
                int major  = (ver >> 16) & 255;
                int minor1 = (ver >> 8) & 255;
                int minor2 = (ver) & 255;
                Logger.LogTrace($"SunVox lib version: {major}.{minor1}.{minor2}", Category.SunVox);

                InitAnnounce();
                InitMusic();
                InitFX();
                Initialized = true;
            }
            else
            {
                Logger.LogWarning("sv_init() error " + ver, Category.SunVox);
            }
        }
        catch (Exception e)
        {
            Logger.LogWarning("Exception: " + e, Category.SunVox);
        }
    }
Beispiel #11
0
    private void InitMusic()
    {
        if (Disabled)
        {
            return;
        }

        SunVox.sv_open_slot((int)Slot.Music);
    }
Beispiel #12
0
    private void InitFX()
    {
        if (Disabled)
        {
            return;
        }

        SunVox.sv_open_slot((int)Slot.FX);
    }
Beispiel #13
0
    public void SetMusicVolume(byte volume)
    {
        if (Disabled)
        {
            return;
        }

        SunVox.sv_volume((int)Slot.Music, volume);
    }
Beispiel #14
0
    private void Stop(Slot slot)
    {
        if (Disabled)
        {
            return;
        }

        SunVox.sv_stop((int)slot);
    }
Beispiel #15
0
    void OnAudioFilterRead(float[] data, int channels)
    {
        int   numSamples = data.Length / channels;
        float time       = (float)numSamples / sampleRate;
        int   ticks      = (int)(time * SunVox.sv_get_ticks_per_second());

        // I think I got this right but I'm not sure?
        // TODO check the docs again... e.g. what is "user_ticks_per_second"?
        SunVox.sv_audio_callback(data, numSamples, numSamples, SunVox.sv_get_ticks() + ticks);
    }
Beispiel #16
0
    private void OnDestroy()
    {
        if (!enabled)
        {
            return;
        }

        SunVox.sv_close_slot(0);
        SunVox.sv_deinit();
    }
Beispiel #17
0
    private IEnumerator ModuleCoroutine()
    {
        //Create Generator module:
        SunVox.sv_lock_slot(0);
        int mod_num = SunVox.sv_new_module(0, "Generator", "Generator", 0, 0, 0);

        SunVox.sv_unlock_slot(0);
        if (mod_num >= 0)
        {
            log("New module created: " + mod_num);
            //Connect the new module to the Main Output:
            SunVox.sv_lock_slot(0);
            SunVox.sv_connect_module(0, mod_num, 0);
            SunVox.sv_unlock_slot(0);
            //Send Note ON:
            log("Note ON");
            SunVox.sv_send_event(0, 0, 64, 128, mod_num + 1, 0, 0);
            yield return(new WaitForSeconds(1));

            //Send Note OFF:
            log("Note OFF");
            SunVox.sv_send_event(0, 0, 128, 128, mod_num + 1, 0, 0);
            yield return(new WaitForSeconds(1));
        }
        else
        {
            log("Can't create the new module");
        }

        //Load module and play it:
        var path     = "Assets/StreamingAssets/organ.sunsynth"; // This path is correct only for standalone
        int mod_num2 = SunVox.sv_load_module(0, path, 0, 0, 0);

        if (mod_num2 >= 0)
        {
            log("Module loaded: " + mod_num2);
            //Connect the new module to the Main Output:
            SunVox.sv_lock_slot(0);
            SunVox.sv_connect_module(0, mod_num2, 0);
            SunVox.sv_unlock_slot(0);
            //Send Note ON:
            log("Note ON");
            SunVox.sv_send_event(0, 0, 64, 128, mod_num2 + 1, 0, 0);
            yield return(new WaitForSeconds(1));

            //Send Note OFF:
            log("Note OFF");
            SunVox.sv_send_event(0, 0, 128, 128, mod_num2 + 1, 0, 0);
            yield return(new WaitForSeconds(1));
        }
        else
        {
            log("Can't load the module");
        }
    }
Beispiel #18
0
 private void FixedUpdate()
 {
     if (SunVox.sv_end_of_song(0) == 0)
     {
         Debug.LogFormat("Line counter: {0} Module 7 -> {1} = {2}",
                         (float)SunVox.sv_get_current_line2(0) / 32,
                         Marshal.PtrToStringAnsi(SunVox.sv_get_module_ctl_name(0, 7, 1)), //Get controller name
                         SunVox.sv_get_module_ctl_value(0, 7, 1, 0)                       //Get controller value
                         );
     }
 }
Beispiel #19
0
    /// <summary>
    /// Checks if music in lobby is being played or not.
    /// <returns> true if music is being played.</returns>
    /// </summary>
    public static bool isLobbyMusicPlaying()
    {
        // Checks if an audiosource or a track by sunvox is being played(Since there are two diiferent ways to play tracks)
        if (currentLobbyAudioSource != null && currentLobbyAudioSource.isPlaying ||
            !(SunVox.sv_end_of_song((int)Slot.Music) == 1))
        {
            return(true);
        }

        return(false);
    }
Beispiel #20
0
    private IEnumerator onBinaryFileLoadedCoroutine(int mod_num)
    {
        SunVox.sv_sampler_load_from_memory(0, mod_num, sunvox_sample, sunvox_sample_size, -1);
        //Send Note ON:
        log("Note ON");
        SunVox.sv_send_event(0, 0, 64, 128, mod_num + 1, 0, 0);
        yield return(new WaitForSeconds(1));

        //Send Note OFF:
        log("Note OFF");
        SunVox.sv_send_event(0, 0, 128, 128, mod_num + 1, 0, 0);
    }
Beispiel #21
0
    private IEnumerator VolumeUpdateCoroutine()
    {
        yield return(null); // wait a frame to allow world to finish loading

        if (playMode == PlayMode.BKGND)
        {
            SunVox.sv_play_from_beginning(slot);
        }

        while (true)
        {
            if (fadingIn)
            {
                if (fadeIn == 0)
                {
                    currentVolume = volume;
                }
                else
                {
                    currentVolume += volume / fadeIn * Time.unscaledDeltaTime;
                }
                if (currentVolume >= volume)
                {
                    currentVolume = volume;
                    fadingIn      = false;
                }
                UpdateSunVoxVolume();
            }
            else if (fadingOut)
            {
                if (fadeOut == 0)
                {
                    currentVolume = 0;
                }
                else
                {
                    currentVolume -= volume / fadeOut * Time.unscaledDeltaTime;
                }
                if (currentVolume <= 0)
                {
                    currentVolume = 0;
                    fadingOut     = false;
                    if (playMode != PlayMode.BKGND)
                    {
                        SunVox.sv_stop(slot);
                    }
                }
                UpdateSunVoxVolume();
            }

            yield return(null);
        }
    }
Beispiel #22
0
    public SunVoxPlayer(byte[] data)
    {
        slot = SunVoxUtils.OpenUnusedSlot();
        int result = SunVox.sv_load_from_memory(slot, data, data.Length);

        if (result != 0)
        {
            Debug.LogError("Error loading file");
            return;
        }
        SunVox.sv_play_from_beginning(slot);
    }
 /// <summary>
 /// Checks if music is being played or not.
 /// <returns> true if music is being played.</returns>
 /// </summary>
 public static bool isMusicPlaying()
 {
     if (Instance.musicAudioSource != null &&
         Instance.musicAudioSource.isPlaying ||
         (SunVox.sv_end_of_song((int)Slot.Music) != 0))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Beispiel #24
0
    private void onBinaryFileLoaded()
    {
        int mod_num2 = SunVox.sv_load_module_from_memory(0, sunvox_module, sunvox_module_size, 0, 0, 0);

        if (mod_num2 >= 0)
        {
            StartCoroutine(playModuleCoroutine(mod_num2));
        }
        else
        {
            log("Load error.");
        }
    }
 /// <summary>
 /// Checks if music in lobby is being played or not.
 /// <returns> true if music is being played.</returns>
 /// </summary>
 public static bool isLobbyMusicPlaying()
 {
     if (Instance.currentLobbyAudioSource != null &&
         Instance.currentLobbyAudioSource.isPlaying ||
         !(SunVox.sv_end_of_song((int)Slot.Music) == 1))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Beispiel #26
0
    private void OnDestroy()
    {
        if (!enabled)
        {
            return;
        }

        foreach (Slot slot in Enum.GetValues(typeof(Slot)))
        {
            SunVox.sv_close_slot((int)slot);
        }
        SunVox.sv_deinit();
    }
Beispiel #27
0
 public void PlayAnnouncement(byte[] sound)
 {
     try {
         SunVox.sv_stop((int)Slot.Announce);
         SunVox.sv_sampler_load_from_memory((int)Slot.Announce, SamplerModule, sound, sound.Length, -1);
         SunVox.sv_set_autostop((int)Slot.Announce, 1);
         //play announcement tune
         SunVox.sv_play_from_beginning((int)Slot.Announce);
         //speak tts with effects
         StartCoroutine(SpeakAnnouncement());
     } catch (Exception e) {
         Logger.LogWarning("Exception: " + e, Category.SunVox);
     }
 }
Beispiel #28
0
 private void Update()
 {
     if (Input.GetKeyDown(KeyCode.Space))
     {
         if (SunVox.sv_end_of_song(0) == 1)
         {
             SunVox.sv_play(0);
         }
         else
         {
             SunVox.sv_stop(0);
         }
     }
 }
Beispiel #29
0
    private IEnumerator playModuleCoroutine(int mod_num2)
    {
        log("Module loaded: " + mod_num2);
        //Connect the new module to the Main Output:
        SunVox.sv_lock_slot(0);
        SunVox.sv_connect_module(0, mod_num2, 0);
        SunVox.sv_unlock_slot(0);
        //Send Note ON:
        log("Note ON");
        SunVox.sv_send_event(0, 0, 64, 128, mod_num2 + 1, 0, 0);
        yield return(new WaitForSeconds(1));

        //Send Note OFF:
        log("Note OFF");
        SunVox.sv_send_event(0, 0, 128, 128, mod_num2 + 1, 0, 0);
    }
Beispiel #30
0
    private void InitAnnounce()
    {
        SunVox.sv_open_slot((int)Slot.Announce);

        Logger.LogTrace("Loading Announce project from file", Category.SunVox);
//		var path = "Assets/StreamingAssets/announcement2.sunvox";
        var path = GetDataPath("announcement2.sunvox");

        if (SunVox.sv_load((int)Slot.Announce, path) == 0)
        {
//			log( "Loaded." );
        }
        else
        {
            Logger.LogWarning($"Announce project load error: {path}", Category.SunVox);
//			SunVox.sv_volume( (int)Slot.Announce, 256 );
        }
    }