Skip to content

Instantly share code, notes, and snippets.

@Pyredrid
Created September 21, 2016 23:54
Show Gist options
  • Save Pyredrid/d50cbcfd4318c6e846fc74dcb60477c4 to your computer and use it in GitHub Desktop.
Save Pyredrid/d50cbcfd4318c6e846fc74dcb60477c4 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
public static class SunVoxWrapper {
private const string libPath = "sunvox";
static SunVoxWrapper () {
Init("", 44100, 2, 0);
}
//sv_audio_callback() - get the next piece of SunVox audio.
//With sv_audio_callback() you can ignore the built-in SunVox sound output mechanism and use some other sound system.
//Set SV_INIT_FLAG_USER_AUDIO_CALLBACK flag in sv_init() if you want to use sv_audio_callback() function.
//Parameters:
// buf - destination buffer of type signed short (if SV_INIT_FLAG_AUDIO_INT16 used in sv_init())
// or float (if SV_INIT_FLAG_AUDIO_FLOAT32 used in sv_init());
// stereo data will be interleaved in this buffer: LRLR... ; where the LR is the one frame (Left+Right channels);
// frames - number of frames in destination buffer;
// latency - audio latency (in frames);
// out_time - output time (in ticks).
[DllImport(libPath, EntryPoint = "sv_audio_callback")]
public static extern int AudioCallback (IntPtr buf, int frames, int latency, int outTime);
[DllImport(libPath, EntryPoint = "sv_open_slot")]
public static extern int OpenSlot (int slot);
[DllImport(libPath, EntryPoint = "sv_close_slot")]
public static extern int CloseSlot (int slot);
[DllImport(libPath, EntryPoint = "sv_lock_slot")]
public static extern int LockSlot (int slot);
[DllImport(libPath, EntryPoint = "sv_unlock_slot")]
public static extern int UnlockSlot (int slot);
[DllImport(libPath, EntryPoint = "sv_init")]
public static extern int Init (string dev, int freq, int channels, int flags);
[DllImport(libPath, EntryPoint = "sv_deinit")]
public static extern int DeInit ();
//sv_get_sample_type() - get internal sample type of the SunVox engine. Return value: one of the SV_STYPE_xxx defines.
//Use it to get the scope buffer type from get_module_scope() function.
[DllImport(libPath, EntryPoint = "sv_get_sample_type")]
public static extern int GetSampleType ();
[DllImport(libPath, EntryPoint = "sv_load")]
public static extern int Load (int slot, string filePath);
[DllImport(libPath, EntryPoint = "sv_play")]
public static extern int Play (int slot);
[DllImport(libPath, EntryPoint = "sv_play_from_beginning")]
public static extern int PlayFromBeginning (int slot);
[DllImport(libPath, EntryPoint = "sv_stop")]
public static extern int Stop (int slot);
[DllImport(libPath, EntryPoint = "sv_set_autostop")]
public static extern int SetAutoStop (int slot, int autostop);
[DllImport(libPath, EntryPoint = "sv_end_of_song")]
public static extern int EndOfSong (int slot);
[DllImport(libPath, EntryPoint = "sv_rewind")]
public static extern int Rewind (int slot, int lineNum);
[DllImport(libPath, EntryPoint = "sv_volume")]
public static extern int Volume (int slot, int volume);
//ctl - 0xCCEE. CC - number of a controller (1..255). EE - std effect
//ctl_val - value of controller/effect
[DllImport(libPath, EntryPoint = "sv_send_event")]
public static extern int SendEvent (int slot, int track_num, int note, int vel, int module, int ctl, int ctl_val);
[DllImport(libPath, EntryPoint = "sv_get_current_line")]
public static extern int GetCurrentLine (int slot); //Get current line number
[DllImport(libPath, EntryPoint = "sv_get_current_line2")]
public static extern int GetCurrentLine2 (int slot); //Get current line number in fixed point format 27.5
[DllImport(libPath, EntryPoint = "sv_get_current_signal_level")]
public static extern int GetCurrentSignalLevel (int slot, int channel); //From 0 to 255
[DllImport(libPath, EntryPoint = "sv_get_song_name")]
public static extern string GetSongName (int slot);
[DllImport(libPath, EntryPoint = "sv_get_song_bpm")]
public static extern int GetSongBPM (int slot);
[DllImport(libPath, EntryPoint = "sv_get_song_tpl")]
public static extern int GetSongTPL (int slot);
//Frame is one discrete of the sound. Sampling frequency 44100 Hz means, that you hear 44100 frames per second.
[DllImport(libPath, EntryPoint = "sv_get_song_length_frames")]
public static extern int GetSongLengthFrames (int slot);
[DllImport(libPath, EntryPoint = "sv_get_song_length_lines")]
public static extern int GetSongLengthLines (int slot);
[DllImport(libPath, EntryPoint = "sv_new_module")]
//sv_new_module() - create a new module;
public static extern int NewModule (int slot, string type, string name, int x, int y, int z ); //USE LOCK/UNLOCK!
[DllImport(libPath, EntryPoint = "sv_remove_module")]
//sv_remove_module() - remove selected module;
public static extern int RemoveModule (int slot, int mod_num); //USE LOCK/UNLOCK!
[DllImport(libPath, EntryPoint = "sv_connect_module")]
//sv_connect_module() - connect the source to the destination;
public static extern int ConnectModule (int slot, int source, int destination); //USE LOCK/UNLOCK!
[DllImport(libPath, EntryPoint = "sv_disconnect_module")]
//sv_disconnect_module() - disconnect the source from the destination;
public static extern int DisconnectModule (int slot, int source, int destination); //USE LOCK/UNLOCK!
[DllImport(libPath, EntryPoint = "sv_load_module")]
//sv_load_module() - load a module; supported file formats: sunsynth, xi, wav, aiff;
public static extern int LoadModule (int slot, string filePath, int x, int y, int z );
[DllImport(libPath, EntryPoint = "sv_sampler_load")]
//sv_sampler_load() - load a sample to already created Sampler; to replace the whole sampler - set sample_slot to -1;
public static extern int SamplerLoad (int slot, int samplerModule, string filePath, int sampleSlot );
[DllImport(libPath, EntryPoint = "sv_get_number_of_modules")]
public static extern int GetNumberOfModules (int slot);
[DllImport(libPath, EntryPoint = "sv_get_module_flags")]
public static extern int GetModuleFlags (int slot, int modNum);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment