static void play_note(struct SoundPlugin *plugin, int64_t time, float note_num, int64_t note_id, float volume, float pan){ Data *data = (Data*)plugin->data; //printf("Playing %d\n",note_num); Voice *voice = data->voices_not_playing; if(voice==NULL){ printf("no more free voices\n"); return; } RT_remove_voice(&data->voices_not_playing, voice); RT_add_voice(&data->voices_playing, voice); //voice->dsp_instance->init((int)data->samplerate); *(voice->myUI._gate_control) = 1.0f; *(voice->myUI._freq_control) = midi_to_hz(note_num); *(voice->myUI._gain_control) = velocity2gain(volume); voice->note_num = note_num; voice->note_id = note_id; voice->frames_since_stop = 0; voice->delta_pos_at_start = time; voice->delta_pos_at_end = -1; }
static void RT_process_instrument2(int num_outputs, Data *data, int64_t time, int num_frames, float **inputs, float **outputs){ for(int i=0;i<num_outputs;i++) memset(outputs[i],0,num_frames*sizeof(float)); float *tempsounds[num_outputs]; float tempdata[num_outputs][num_frames]; for(int i=0;i<num_outputs;i++) tempsounds[i] = &tempdata[i][0]; Voice *voice = data->voices_playing; //printf("Voices? %s\n",voice==NULL?"No":"Yes"); while(voice!=NULL){ Voice *next = voice->next; int start_process; if(RT_play_voice(data,voice,num_frames,inputs,tempsounds,&start_process)==VOICE_REMOVE){ RT_remove_voice(&data->voices_playing, voice); RT_add_voice(&data->voices_not_playing, voice); for(int ch=0;ch<num_outputs;ch++) RT_fade_out(tempsounds[ch], num_frames-start_process); } for(int ch=0;ch<num_outputs;ch++){ float *source = tempsounds[ch]; float *target = outputs[ch]; for(int i=start_process;i<num_frames;i++) target[i] += source[i]; } voice=next; } }
static void RT_process(SoundPlugin *plugin, int64_t time, int num_frames, float **inputs, float **outputs){ Data *data = (Data*)plugin->data; int num_outputs = plugin->type->num_outputs; for(int i=0;i<num_outputs;i++) memset(outputs[i],0,num_frames*sizeof(float)); float tempsound[num_frames]; Voice *voice = data->voices_playing; while(voice!=NULL){ Voice *next = voice->next; if(RT_play_voice(data, voice, num_frames, tempsound)==VOICE_REMOVE){ RT_remove_voice(&data->voices_playing, voice); RT_add_voice(&data->voices_not_playing, voice); } for(int ch=0;ch<num_outputs;ch++){ float *source = tempsounds[ch]; float *target = outputs[ch]; for(int i=0 ; i<num_frames ; i++) target[i] += tempsound[i]; // TODO: Apply panning } voice = next; } }
static void play_note2(Data *data, int time, note_t note){ //printf("Playing %f\n",note.pitch); Voice *voice = data->voices_not_playing; if(voice==NULL){ printf("no more free voices\n"); return; } RT_remove_voice(&data->voices_not_playing, voice); RT_add_voice(&data->voices_playing, voice); *(voice->myUI._gate_control) = 1.0f; *(voice->myUI._freq_control) = midi_to_hz(note.pitch); *(voice->myUI._gain_control) = velocity2gain(note.velocity); voice->note_num = note.pitch; voice->note_id = note.id; voice->seqblock = note.seqblock; voice->frames_since_stop = 0; voice->delta_pos_at_start = time; voice->delta_pos_at_end = -1; }
static void RT_process(SoundPlugin *plugin, int64_t time, int num_frames, float **inputs, float **outputs){ Data *data = (Data*)plugin->data; Voice *voice = data->voices_playing; memset(outputs[0],0,num_frames*sizeof(float)); memset(outputs[1],0,num_frames*sizeof(float)); while(voice!=NULL){ Voice *next = voice->next; if(RT_play_voice(data, voice, num_frames, outputs)==true){ RT_remove_voice(&data->voices_playing, voice); RT_add_voice(&data->voices_not_playing, voice); } voice = next; } if(data->new_data != NULL){ RT_fade_out(outputs[0],num_frames); RT_fade_out(outputs[1],num_frames); plugin->data = data->new_data; // Bang! (hmm.) data->new_data = NULL; RSEMAPHORE_signal(data->signal_from_RT,1); } }
// May be called from any thread static void convert_effect_data_to_instrument_data(Data *data, dsp *initialized_dsps[MAX_POLYPHONY]){ for(int i=0;i<MAX_POLYPHONY;i++){ Voice *voice = &data->voices[i]; voice->dsp_instance = initialized_dsps[i]; voice->dsp_instance->buildUserInterface(&voice->myUI); voice->myUI.remove_instrument_notecontrol_effects(); voice->myUI.uniqifyEffectNames(); RT_add_voice(&data->voices_not_playing, voice); } }
static void play_note(struct SoundPlugin *plugin, int64_t time, float note_num, int64_t note_id, float volume, float pan){ Data *data = (Data*)plugin->data; //fprintf(stderr,"playing note %d. Pitch: %d, time: %d\n",(int)note_id,(int)note_num,(int)time); const Note *note = &data->notes[(int)note_num]; int i; for(i=0;i<note->num_samples;i++){ if(data->voices_not_playing==NULL){ printf("No more free voices\n"); return; } Voice *voice = data->voices_not_playing; RT_remove_voice(&data->voices_not_playing, voice); RT_add_voice(&data->voices_playing, voice); voice->last_finetune_value = data->finetune; voice->note_num = note_num; voice->note_id = note_id; voice->start_volume = velocity2gain(volume); voice->end_volume = voice->start_volume; voice->start_pitch = note_num; voice->end_pitch = note_num; voice->sample = note->samples[i]; if(data->loop_onoff==true && voice->sample->loop_end > voice->sample->loop_start) voice->pos=scale(data->startpos, // set startpos between 0 and loop_end 0,1, 0,voice->sample->loop_end); else voice->pos=scale(data->startpos, // set startpos between 0 and sound length 0,1, 0,voice->sample->num_frames); voice->pan = get_pan_vals_vector(pan,voice->sample->ch==-1?1:2); RESAMPLER_reset(voice->resampler); ADSR_reset(voice->adsr); ADSR_set_adsr(voice->adsr, data->a, data->h, data->d, data->s, data->r); voice->delta_pos_at_start=time; voice->delta_pos_at_end=-1; voice->is_fading_out=false; } }
static void *create_plugin_data(const SoundPluginType *plugin_type, SoundPlugin *plugin, hash_t *state, float sample_rate, int block_size){ Data *data = (Data*)V_calloc(1,sizeof(Data)); data->resampler_type = RESAMPLER_SINC1; for(int i=0;i<POLYPHONY;i++){ Voice *voice = &data->voices[i]; voice->resampler = RESAMPLER_create(RT_src_callback, 1, voice, data->resampler_type); RT_add_voice(&data->voices_not_playing, voice); } return data; }
static void *create_instrument_plugin_data(const SoundPluginType *plugin_type, struct SoundPlugin *plugin, float samplerate, int blocksize){ Data *data = new Data; for(int i=0;i<MAX_POLYPHONY;i++){ Voice *voice = &data->voices[i]; voice->dsp_instance = new CLASSNAME; voice->dsp_instance->init(samplerate); voice->dsp_instance->buildUserInterface(&voice->myUI); voice->myUI.remove_instrument_notecontrol_effects(); RT_add_voice(&data->voices_not_playing, voice); } return data; }
static void play_note(struct SoundPlugin *plugin, int64_t time, float note_num, int64_t note_id, float volume,float pan){ Data *data = (Data*)plugin->data; if(data->voices_not_playing==NULL){ printf("No more free voices\n"); return; } Voice *voice = data->voices_not_playing; RT_remove_voice(&data->voices_not_playing, voice); RT_add_voice(&data->voices_playing, voice); voicd->note_id = note_id; voice->velocity = volume; voice->pan = get_pan_vals_vector(pan,2); voice->delta_pos_at_start=time; voice->delta_pos_at_end=-1; voice->audio = data->audio[0] }
static void play_note(struct SoundPlugin *plugin, int64_t time, int note_num, float volume){ Data *data = (Data*)plugin->data; //printf("Playing %d\n",note_num); Voice *voice = data->voices_not_playing; if(voice==NULL){ printf("no more free voices\n"); return; } RT_remove_voice(&data->voices_not_playing, voice); RT_add_voice(&data->voices_playing, voice); *(voice->myUI._gate_control) = 1.0f; *(voice->myUI._freq_control) = midi_to_hz(note_num); *(voice->myUI._gain_control) = velocity2gain(volume); voice->note_num = note_num; }
static bool load_sample(Data *data, const wchar_t *filename, int instrument_number){ if(load_xi_instrument(data,filename)==false) if(load_sample_with_libsndfile(data,filename)==false) if(load_sf2_instrument(data,filename,instrument_number)==false) return false; //data->num_channels = data->samples[0].num_channels; // All samples must contain the same number of channels. generate_peaks(data); int i=0; for(i=0;i<POLYPHONY;i++){ Voice *voice = &data->voices[i]; voice->resampler = RESAMPLER_create(RT_src_callback, 1, voice, data->resampler_type); voice->adsr = ADSR_create(data->samplerate); RT_add_voice(&data->voices_not_playing, voice); } return true; }