2022-07-10 11:32:21 -05:00
|
|
|
#include "music.h"
|
|
|
|
|
|
|
|
#include "dsp.h"
|
|
|
|
#include "tsf.h"
|
|
|
|
#include "tml.h"
|
|
|
|
#include "mix.h"
|
|
|
|
#include "sound.h"
|
2022-07-12 01:28:41 -05:00
|
|
|
#include "log.h"
|
2022-07-10 11:32:21 -05:00
|
|
|
|
2022-07-10 13:04:24 -05:00
|
|
|
#define TSF_BLOCK 32
|
2022-07-10 11:32:21 -05:00
|
|
|
|
|
|
|
struct dsp_midi_song gsong;
|
|
|
|
|
2022-07-11 23:21:57 -05:00
|
|
|
float music_pan = 0.f;
|
|
|
|
|
2022-07-10 11:32:21 -05:00
|
|
|
void dsp_midi_fillbuf(struct dsp_midi_song *song, void *out, int n)
|
|
|
|
{
|
2023-08-29 17:11:36 -05:00
|
|
|
soundbyte *o = (soundbyte*)out;
|
|
|
|
tml_message *midi = song->midi;
|
|
|
|
|
|
|
|
for (int i = 0; i < n; i += TSF_BLOCK) {
|
|
|
|
while (midi && song->time >= midi->time) {
|
|
|
|
switch (midi->type) {
|
|
|
|
case TML_PROGRAM_CHANGE:
|
|
|
|
tsf_channel_set_presetnumber(song->sf, midi->channel, midi->program, (midi->channel == 9));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TML_NOTE_ON:
|
|
|
|
tsf_channel_note_on(song->sf, midi->channel, midi->key, midi->velocity / 127.f);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TML_NOTE_OFF:
|
|
|
|
tsf_channel_note_off(song->sf, midi->channel, midi->key);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TML_PITCH_BEND:
|
|
|
|
tsf_channel_set_pitchwheel(song->sf, midi->channel, midi->pitch_bend);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TML_CONTROL_CHANGE:
|
|
|
|
tsf_channel_midi_control(song->sf, midi->channel, midi->control, midi->control_value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
midi = midi->next;
|
2022-07-10 11:32:21 -05:00
|
|
|
}
|
2023-08-29 17:11:36 -05:00
|
|
|
tsf_render_float(song->sf, o, TSF_BLOCK, 0);
|
|
|
|
o += TSF_BLOCK*CHANNELS;
|
|
|
|
song->time += TSF_BLOCK * (1000.f/SAMPLERATE);
|
|
|
|
}
|
2022-07-10 11:32:21 -05:00
|
|
|
|
2023-08-29 17:11:36 -05:00
|
|
|
song->midi = midi;
|
2022-07-11 23:21:57 -05:00
|
|
|
|
2023-08-29 17:11:36 -05:00
|
|
|
// dsp_pan(&music_pan, out, n);
|
2022-07-10 11:32:21 -05:00
|
|
|
}
|
|
|
|
|
2023-01-15 23:27:28 -06:00
|
|
|
struct bus *musicbus;
|
|
|
|
|
2022-07-10 11:32:21 -05:00
|
|
|
void play_song(const char *midi, const char *sf)
|
|
|
|
{
|
2022-07-12 01:28:41 -05:00
|
|
|
gsong.midi = tml_load_filename(midi);
|
|
|
|
if (gsong.midi == NULL) {
|
|
|
|
YughWarn("Midi %s not found.", midi);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gsong.sf = tsf_load_filename(sf);
|
|
|
|
|
|
|
|
if (gsong.sf == NULL) {
|
|
|
|
YughWarn("SF2 %s not found.", sf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-10 11:32:21 -05:00
|
|
|
gsong.time = 0.f;
|
|
|
|
|
|
|
|
tsf_set_output(gsong.sf, TSF_STEREO_INTERLEAVED, SAMPLERATE, 0.f);
|
|
|
|
|
|
|
|
// Preset on 10th MIDI channel to use percussion sound bank if possible
|
|
|
|
tsf_channel_set_bank_preset(gsong.sf, 9, 128, 0);
|
|
|
|
|
2023-09-13 07:31:00 -05:00
|
|
|
struct dsp_filter cursong;
|
2022-07-10 11:32:21 -05:00
|
|
|
cursong.data = &gsong;
|
|
|
|
cursong.filter = dsp_midi_fillbuf;
|
2023-01-15 23:27:28 -06:00
|
|
|
musicbus = first_free_bus(cursong);
|
2022-07-10 11:32:21 -05:00
|
|
|
}
|
2022-07-28 14:07:20 -05:00
|
|
|
|
|
|
|
void music_play()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void music_stop()
|
|
|
|
{
|
2023-01-15 23:27:28 -06:00
|
|
|
bus_free(musicbus);
|
2022-07-28 14:07:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void music_volume()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void music_resume()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void music_paused()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void music_pause()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void sound_play()
|
|
|
|
{
|
|
|
|
|
2022-11-25 07:12:31 -06:00
|
|
|
}
|