Ejemplo n.º 1
0
SynthSequencer::SynthSequencer(QObject *parent)
    : QObject(parent)
    , m_engine(0)
    , m_synthID(0)
{
    m_sequencer = new_fluid_sequencer2(0);
}
Ejemplo n.º 2
0
	bool init(MidiFormat format, const char* patchLoc)
	{
		free();
		s_mutex = Mutex::create();
		s_midiFormat = format;

		if (s_midiFormat == MFMT_GUS_PATCH)
		{
			s_volume = c_volumeScale[0];
			s_sampleRate = 32072.0;
			if (WildMidi_Init(patchLoc, 32072, WM_MO_ENHANCED_RESAMPLING) >= 0)
			{
				s_initialized = true;
				WildMidi_MasterVolume(100);
				
				return true;
			}
		}
		else if (s_midiFormat == MFMT_SOUND_FONT)
		{
			s_volume = c_volumeScale[1];
			if (!loadFluidsythDLL())
			{
				LOG( LOG_ERROR, "cannot find or load the \"libfluidsynth\" dynamic library." );
				return false;
			}
			
			s_fluidSettings = new_fluid_settings();
			fluid_settings_setstr(s_fluidSettings, "player.timing-source", "sample");
			fluid_settings_setstr(s_fluidSettings, "synth.lock-memory", 0);
			fluid_settings_setstr(s_fluidSettings, "synth.chorus-active", "0");

			s_fluidSynth = new_fluid_synth(s_fluidSettings);
			if (fluid_synth_sfload(s_fluidSynth, patchLoc, 1) < 0)
			{
				LOG( LOG_ERROR, "cannot load sound font \"%s\"", patchLoc );
				unloadFluidsynthDLL();
				return false;
			}
			
			s_fluidSeq = new_fluid_sequencer2(false);
			fluid_sequencer_register_fluidsynth(s_fluidSeq, s_fluidSynth);
			fluid_settings_getnum(s_fluidSettings, "synth.sample-rate", &s_sampleRate);

			s_initialized = true;
			s_fluidPlayer = new_fluid_player(s_fluidSynth);
			return true;
		}

		return false;
	}
Ejemplo n.º 3
0
	size_t load_buffer(ALuint buf) {
		if (is_end()) {
			source_.seq.reset(new_fluid_sequencer2(false), &delete_fluid_sequencer);
			BOOST_VERIFY(fluid_sequencer_register_fluidsynth(source_.seq.get(),
			                                                 source_.synth.get()) != FLUID_FAILED);

			BOOST_VERIFY(fluid_player_add(source_.player.get(), filename_.c_str()) != FLUID_FAILED);
		}

		data_.resize(2 * source_.sample_rate * SECOND_PER_BUFFER);
		if (fluid_synth_write_s16(source_.synth.get(), data_.size() / 2, &data_.front(), 0, 2,
		                          &data_.front(), 1, 2) == FLUID_FAILED) {
			Output::Error("synth error: %s", fluid_synth_error(source_.synth.get()));
		}
		alBufferData(buf, AL_FORMAT_STEREO16, &data_.front(), sizeof(int16_t) * data_.size(),
		             source_.sample_rate);

		return data_.size() / 2;
	}
Ejemplo n.º 4
0
	void init_midi() {
		if (synth) {
			return;
		}

		settings.reset(new_fluid_settings(), &delete_fluid_settings);
		fluid_settings_setstr(settings.get(), "player.timing-source", "sample");
		fluid_settings_setint(settings.get(), "synth.lock-memory", 0);

		synth.reset(new_fluid_synth(settings.get()), &delete_fluid_synth);
		BOOST_VERIFY(fluid_synth_sfload(synth.get(), getenv("DEFAULT_SOUNDFONT"), 1) !=
		             FLUID_FAILED);

		double sample_rate = 0;
		fluid_settings_getnum(settings.get(), "synth.sample-rate", &sample_rate);
		BOOST_ASSERT(sample_rate != 0);
		this->sample_rate = sample_rate;

		seq.reset(new_fluid_sequencer2(false), &delete_fluid_sequencer);
		BOOST_VERIFY(fluid_sequencer_register_fluidsynth(seq.get(), synth.get()) != FLUID_FAILED);
	}
Ejemplo n.º 5
0
	void init_midi() {
		if (synth) {
			return;
		}

		settings.reset(new_fluid_settings(), &delete_fluid_settings);
		fluid_settings_setstr(settings.get(), "player.timing-source", "sample");
		fluid_settings_setint(settings.get(), "synth.lock-memory", 0);

		synth.reset(new_fluid_synth(settings.get()), &delete_fluid_synth);
		if (fluid_synth_sfload(synth.get(), getenv("DEFAULT_SOUNDFONT"), 1) == FLUID_FAILED)
			Output::Error("Couldn't load soundfont\n%s.", getenv("DEFAULT_SOUNDFONT"));

		double sample_rate = 0;
		fluid_settings_getnum(settings.get(), "synth.sample-rate", &sample_rate);
		assert(sample_rate != 0);
		this->sample_rate = sample_rate;

		seq.reset(new_fluid_sequencer2(false), &delete_fluid_sequencer);
		if (fluid_sequencer_register_fluidsynth(seq.get(), synth.get()) == FLUID_FAILED)
			Output::Error("Couldn't initialize MIDI playback.");
	}
Ejemplo n.º 6
0
/**
 * Create a new sequencer object which uses the system timer.  Use
 * new_fluid_sequencer2() to specify whether the system timer or
 * fluid_sequencer_process() is used to advance the sequencer.
 * @return New sequencer instance
 */
fluid_sequencer_t*
new_fluid_sequencer (void)
{
	return new_fluid_sequencer2 (TRUE);
}
Ejemplo n.º 7
0
static void *create_data(const wchar_t *filename, float samplerate){
  Data *data = calloc(1,sizeof(Data));

  data->pitch = 16384/2;
  data->pitch_range = 1;
  data->modulation = 0;
  data->sustain_on = 0;

  data->signal_from_RT = RSEMAPHORE_create(0);

  data->settings = new_fluid_settings();
  if(data->settings==NULL){
    RError("Unable to create fluidsynth settings");
    delete_data(data);
    return NULL;
  }

  if(fluid_settings_setnum(data->settings, "synth.sample-rate", samplerate)==0){
    RError("Unable to set sample rate of fluidsynth to %f\n",samplerate);
    //delete_data(data);
    //return NULL;
  }

  if(fluid_settings_setint(data->settings, "synth.threadsafe-api", 0)==0){
    printf("Unable to set threadsafe-api to 0 (we don't need it)\n");
  }

  if(fluid_settings_setint(data->settings, "synth.chorus.active", 0)==0){
    printf("Unable to set synth.chorus.active to 0 (we don't use it)\n");
  }

  if(fluid_settings_setint(data->settings, "synth.reverb.active", 0)==0){
    printf("Unable to set synth.reverb.active to 0 (we don't use it)\n");
  }



  // TODO: Decide whether we need fluidsynth's reverb and chorus.
  //fluid_settings_setstr(settings, "synth.reverb.active", "yes");
  //fluid_settings_setstr(settings, "synth.chorus.active", "no");

  data->synth = new_fluid_synth(data->settings);
  if(data->synth==NULL){
    RError("Unable to create fluidsynth synth");
    delete_data(data);
    return NULL;
  }

  data->sequencer = new_fluid_sequencer2(0);
  if(data->sequencer==NULL){
    RError("Unable to create fluidsynth sequencer");
    delete_data(data);
    return NULL;
  }

  fluid_sequencer_set_time_scale(data->sequencer, samplerate); // it's a shame that time in fluidsynth is only 32 bit. Hopefully the values wrap properly around.......
  data->time_scale = fluid_sequencer_get_time_scale(data->sequencer);
  data->samplerate = samplerate;

#if 0 // This test always succeeds. Max time_scale in fluidsynth is 1000.0. :-(
  if( data->time_scale != sample_rate){
    RError("Could not set time scale to %f (set to %f instead)\n",(float)samplerate,(float)fluid_sequencer_get_time_scale(data->sequencer));
  }
#endif

  data->synth_seq_ID = fluid_sequencer_register_fluidsynth(data->sequencer, data->synth);
  data->event = new_fluid_event();
  if(data->event==NULL){
    RError("Unable to create fluidsynth event");
    delete_data(data);
    return NULL;
  }

  data->filename = wcsdup(OS_loading_get_resolved_file_path(filename));

  data->soundfont_id = fluid_synth_sfload(data->synth,STRING_get_chars(data->filename),true);

  if(data->soundfont_id==FLUID_FAILED){
    printf("Soundfont loading failed for \"%s\"\n",STRING_get_chars(data->filename));

    delete_data(data);

    return NULL;

  }

  return data;
}
Ejemplo n.º 8
0
static int midoflus_init(void *arg)
{
    int ret;
    char *sfont;
    char *def_sfonts[] = {
	"/usr/share/soundfonts/default.sf2",		// fedora
	"/usr/share/soundfonts/FluidR3_GM.sf2",		// fedora
	"/usr/share/sounds/sf2/FluidR3_GM.sf2.flac",	// ubuntu
	"/usr/share/sounds/sf2/FluidR3_GM.sf2",		// debian
	NULL };
    int use_defsf = 0;

    settings = new_fluid_settings();
    fluid_settings_setint(settings, "synth.lock-memory", 0);
    fluid_settings_setnum(settings, "synth.gain", flus_gain);
    ret = fluid_settings_setint(settings, "synth.threadsafe-api", 1);
    if (ret == 0) {
	warn("fluidsynth: no threadsafe API\n");
	goto err1;
    }
    ret = fluid_settings_getnum(settings, "synth.sample-rate", &flus_srate);
    if (ret == 0) {
	warn("fluidsynth: cannot get samplerate\n");
	goto err1;
    }
    ret = fluid_settings_getstr(settings, "synth.default-soundfont", &sfont);
    if (ret == 0) {
	int i = 0;
	warn("Your fluidsynth is too old\n");
	while (def_sfonts[i]) {
	    if (access(def_sfonts[i], R_OK) == 0) {
		sfont = def_sfonts[i];
		use_defsf = 1;
		break;
	    }
	    i++;
	}
	if (!use_defsf) {
	    error("Your fluidsynth is too old and soundfonts not found\n");
	    goto err1;
	}
    }

    synth = new_fluid_synth(settings);
    ret = fluid_synth_sfload(synth, sfont, TRUE);
    if (ret == FLUID_FAILED) {
	warn("fluidsynth: cannot load soundfont %s\n", sfont);
	if (use_defsf)
	    error("Your fluidsynth is too old\n");
	goto err2;
    }
    fluid_settings_setstr(settings, "synth.midi-bank-select", "gm");
    S_printf("fluidsynth: loaded soundfont %s ID=%i\n", sfont, ret);
    sequencer = new_fluid_sequencer2(0);
    synthSeqID = fluid_sequencer_register_fluidsynth2(sequencer, synth);

    sem_init(&syn_sem, 0, 0);
    pthread_create(&syn_thr, NULL, synth_thread, NULL);
#ifdef HAVE_PTHREAD_SETNAME_NP
    pthread_setname_np(syn_thr, "dosemu: fluid");
#endif
    pcm_stream = pcm_allocate_stream(FLUS_CHANNELS, "MIDI",
	    (void*)MC_MIDI);

    return 1;

err2:
    delete_fluid_synth(synth);
err1:
    delete_fluid_settings(settings);
    return 0;
}