From 7d3884df563e4f4ff1771cb19ebab896509366c7 Mon Sep 17 00:00:00 2001 From: Tero Lindeman Date: Sat, 14 Mar 2020 09:20:59 +0200 Subject: [PATCH 1/2] Add wrapper for ksnd to trigger instruments --- src/lib/ksnd.c | 12 ++++++++++++ src/lib/ksnd.def | 2 ++ src/lib/ksnd.h | 46 +++++++++++++++++++++++++++++++++------------- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/lib/ksnd.c b/src/lib/ksnd.c index a9e9086..88855ca 100644 --- a/src/lib/ksnd.c +++ b/src/lib/ksnd.c @@ -268,3 +268,15 @@ KLYSAPI int KSND_GetPlayTime(KSong *song, int position) { return mus_get_playtime_at(&song->song, position); } + + +KLYSAPI int KSND_TriggerInstrument(KPlayer *player, KSong *song, int instrument, int channel, int note, int panning) +{ + return mus_trigger_instrument(player->mus, channel, &player->mus->instrument[instrument], note, panning); +} + + +KLYSAPI void KSND_ReleaseNote(KPlayer *player, int channel) +{ + mus_release(player, channel); +} diff --git a/src/lib/ksnd.def b/src/lib/ksnd.def index 8040daf..74a5d40 100644 --- a/src/lib/ksnd.def +++ b/src/lib/ksnd.def @@ -17,3 +17,5 @@ KSND_GetVUMeters KSND_GetSongInfo KSND_SetLooping KSND_GetPlayTime +KSND_TriggerInstrument +KSND_ReleaseNote diff --git a/src/lib/ksnd.h b/src/lib/ksnd.h index 0f7b4c5..070a56a 100644 --- a/src/lib/ksnd.h +++ b/src/lib/ksnd.h @@ -28,7 +28,7 @@ typedef struct KPlayer_t KPlayer; /** * Song information returned by KSND_GetSongInfo() */ -typedef struct +typedef struct { char *song_title; /**< Song title as a null-terminated string */ char *instrument_name[128]; /**< Instrument names as an array */ @@ -37,14 +37,14 @@ typedef struct } KSongInfo; -#ifdef WIN32 +#ifdef WIN32 #ifdef DLLEXPORT #define KLYSAPI _cdecl __declspec(dllexport) #else -#define KLYSAPI +#define KLYSAPI #endif #else -#define KLYSAPI +#define KLYSAPI #endif /** @@ -82,11 +82,11 @@ KLYSAPI extern int KSND_GetSongLength(const KSong *song); * Get song information from a @c KSong. * * If @c NULL is passed as the @a data pointer, an internal, thread-unsafe buffer will - * be used to store the data. This internal buffer will also change every time this - * function is called, thus it is preferable to supply your own @c KSongInfo or + * be used to store the data. This internal buffer will also change every time this + * function is called, thus it is preferable to supply your own @c KSongInfo or * otherwise copy the returned data before a new call. * - * If @c song is freed using KSND_FreeSong(), the corresponding @c KSongInfo may be + * If @c song is freed using KSND_FreeSong(), the corresponding @c KSongInfo may be * invalidated and point to unallocated memory. * * @param song @c KSong whose information is queried @@ -97,13 +97,13 @@ KLYSAPI extern const KSongInfo * KSND_GetSongInfo(KSong *song, KSongInfo *data); /** * Returns the amount of milliseconds that would need to elapse if the song was played - * from the beginning to pattern row @a position. + * from the beginning to pattern row @a position. * - * Use this in conjunction with KSND_GetPlayPosition() to find out the current playback + * Use this in conjunction with KSND_GetPlayPosition() to find out the current playback * time. Or, use with KSND_GetSongLength() to get the song duration. * * @param song song whose play time is polled - * @param position + * @param position * @return @c position measured in milliseconds */ KLYSAPI extern int KSND_GetPlayTime(KSong *song, int position); @@ -171,7 +171,7 @@ KLYSAPI extern int KSND_FillBuffer(KPlayer *player, short int *buffer, int buffe * Set player oversampling quality. * * Oversample range is [0..4]. 0 implies no oversampling and 4 implies 16-fold oversampling. - * The less oversampling the less CPU intensive the playback is but the sound quality will suffer + * The less oversampling the less CPU intensive the playback is but the sound quality will suffer * for very high frequencies. Can be adjusted realtime. * * @param player @c KPlayer context @@ -191,8 +191,8 @@ KLYSAPI extern void KSND_SetVolume(KPlayer *player, int volume); * Enable or disable song looping. * * If a non-zero value is passed via @c looping, the playback routine will exit KSND_FillBuffer() - * if it encounters the song end. This is useful if you want to detect song end and avoid extra - * audio data in the buffer after the song end. + * if it encounters the song end. This is useful if you want to detect song end and avoid extra + * audio data in the buffer after the song end. * * @param player player context * @param looping looping enabled @@ -218,6 +218,26 @@ KLYSAPI extern int KSND_GetPlayPosition(KPlayer* player); */ KLYSAPI extern void KSND_GetVUMeters(KPlayer *player, int *envelope, int n_channels); +/** + * Trigger an instrument from the song manually. When using this the song file itself sets the number of channels, play rate et cetera + * @param player player context which is currently playing a song set with KSND_PlaySong() + * @param song song which has the instruments and which should currently be playing + * @param instrument the instrument index in the song + * @param channel channel on which the instrument should be triggered on (use -1 to autochoose) + * @param note The note at which the instrument is played. This is a 16-bit value with the semitone in the higher 8 bits and fraction in the lower bits + * @param panning stereo panning for the instrument (zero is center) + * @return the channel on which the instrument should now be playing + */ +KLYSAPI int KSND_TriggerInstrument(KPlayer *player, KSong *song, int instrument, int channel, int note, int panning); + +/** + * Release the currently played note + * + * @param player player context which is currently playing a song set with KSND_PlaySong() + * @param channel release the note played on this channel + */ +KLYSAPI void KSND_ReleaseNote(KPlayer *player, int channel); + #ifdef __cplusplus } #endif From 4f02a6dd330277b85a9802dece8e7f41f3dae9c7 Mon Sep 17 00:00:00 2001 From: Tero Lindeman Date: Sat, 4 Apr 2020 10:14:44 +0300 Subject: [PATCH 2/2] VER_READ referenced SDL_RWread directly --- src/macros.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/macros.h b/src/macros.h index aa59589..691937b 100644 --- a/src/macros.h +++ b/src/macros.h @@ -41,7 +41,7 @@ OTHER DEALINGS IN THE SOFTWARE. block;\ } -#define VER_READ(file_version, first_version, last_version, var, size) VER(file_version, first_version, last_version, SDL_RWread(ctx, var, size == 0 ? sizeof(*var) : size, 1)); +#define VER_READ(file_version, first_version, last_version, var, size) VER(file_version, first_version, last_version, my_RWread(ctx, var, size == 0 ? sizeof(*var) : size, 1)); #define _VER_READ(x, size) VER_READ(version, 0, MUS_VERSION, x, size) #define _VER_WRITE(x, size) fwrite(x, !size ? sizeof(*x) : size, 1, f)