int Server_pm_deinit(Server *self) { int i = 0; PyoPmBackendData *be_data = (PyoPmBackendData *) self->midi_be_data; if (self->withPortMidi == 1) { for (i=0; i<self->midiin_count; i++) { Pm_Close(be_data->midiin[i]); } } if (self->withPortMidiOut == 1) { for (i=0; i<self->midiout_count; i++) { Pm_Close(be_data->midiout[i]); } } if (self->withPortMidi == 1 || self->withPortMidiOut == 1) { if (Pt_Started()) Pt_Stop(); Pm_Terminate(); } self->withPortMidi = 0; self->withPortMidiOut = 0; free(self->midi_be_data); return 0; }
int AudioClock::data1(const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { AudioClock *ac = (AudioClock *)user_data; int s = argv[0]->i; if (s > 0) { if (!Pt_Started()) Pt_Start(ac->interval, ac->render, ac); }else { if (Pt_Started()) Pt_Stop(); } return 0; }
int AudioClock::data2(const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { AudioClock *ac = (AudioClock *)user_data; int i = argv[0]->i; int s = argv[3]->i; if (Pt_Started()) Pt_Stop(); ac->numPackets = 64 * (i/4); ac->interval = (double)ac->numPackets/ac->sampleRate; if (s > 0) { if (!Pt_Started()) Pt_Start(ac->interval, ac->render, ac); }else { if (Pt_Started()) Pt_Stop(); } return 0; }
PmError Pm_OpenOutput(PortMidiStream** stream, PmDeviceID outputDevice, void *outputDriverInfo, long bufferSize, PmTimeProcPtr time_proc, void *time_info, long latency ) { PmInternal *midi; PmError err = pmNoError; pm_hosterror = FALSE; *stream = NULL; /* arg checking */ if (outputDevice < 0 || outputDevice >= pm_descriptor_index) err = pmInvalidDeviceId; else if (!descriptors[outputDevice].pub.output) err = pmBadPtr; else if (descriptors[outputDevice].pub.opened) err = pmBadPtr; if (err != pmNoError) goto error_return; /* create portMidi internal data */ midi = (PmInternal *) pm_alloc(sizeof(PmInternal)); *stream = midi; if (!midi) { err = pmInsufficientMemory; goto error_return; } midi->device_id = outputDevice; midi->write_flag = TRUE; midi->time_proc = time_proc; /* if latency > 0, we need a time reference. If none is provided, use PortTime library */ if (time_proc == NULL && latency != 0) { if (!Pt_Started()) Pt_Start(1, 0, 0); /* time_get does not take a parameter, so coerce */ midi->time_proc = (PmTimeProcPtr) Pt_Time; } midi->time_info = time_info; /* when stream used, this buffer allocated and used by winmm_out_open; deleted by winmm_out_close */ midi->buffer_len = bufferSize; midi->buffer = NULL; midi->head = 0; /* unused by output */ midi->tail = 0; /* unused by output */ /* if latency zero, output immediate (timestamps ignored) */ /* if latency < 0, use 0 but don't return an error */ if (latency < 0) latency = 0; midi->latency = latency; midi->overflow = FALSE; /* not used */ midi->flush = FALSE; /* not used */ midi->sysex_in_progress = FALSE; midi->sysex_message = 0; /* unused by output */ midi->sysex_message_count = 0; /* unused by output */ midi->filters = 0; /* not used for output */ midi->sync_time = 0; midi->first_message = TRUE; midi->dictionary = descriptors[outputDevice].dictionary; descriptors[outputDevice].internalDescriptor = midi; /* open system dependent output device */ err = (*midi->dictionary->open)(midi, outputDriverInfo); if (err) { *stream = NULL; descriptors[outputDevice].internalDescriptor = NULL; /* free portMidi data */ pm_free(midi); } else { /* portMidi input open successful */ descriptors[outputDevice].pub.opened = TRUE; } error_return: return pm_errmsg(err); }
int Server_pm_init(Server *self) { int i = 0, ret = 0; PmError pmerr; if (self->midiActive == 0) { self->withPortMidi = 0; self->withPortMidiOut = 0; return 0; } pmerr = Pm_Initialize(); if (pmerr) { Server_warning(self, "Portmidi warning: could not initialize Portmidi: %s\n", Pm_GetErrorText(pmerr)); self->withPortMidi = 0; self->withPortMidiOut = 0; return -1; } else { Server_debug(self, "Portmidi initialized.\n"); self->withPortMidi = 1; self->withPortMidiOut = 1; } PyoPmBackendData *be_data = (PyoPmBackendData *) malloc(sizeof(PyoPmBackendData *)); self->midi_be_data = (void *) be_data; if (self->withPortMidi == 1) { self->midiin_count = self->midiout_count = 0; int num_devices = Pm_CountDevices(); Server_debug(self, "Portmidi number of devices: %d.\n", num_devices); if (num_devices > 0) { if (self->midi_input < num_devices) { if (self->midi_input == -1) self->midi_input = Pm_GetDefaultInputDeviceID(); Server_debug(self, "Midi input device : %d.\n", self->midi_input); const PmDeviceInfo *info = Pm_GetDeviceInfo(self->midi_input); if (info != NULL) { if (info->input) { pmerr = Pm_OpenInput(&be_data->midiin[0], self->midi_input, NULL, 100, NULL, NULL); if (pmerr) { Server_warning(self, "Portmidi warning: could not open midi input %d (%s): %s\n", self->midi_input, info->name, Pm_GetErrorText(pmerr)); self->withPortMidi = 0; } else { Server_debug(self, "Midi input (%s) opened.\n", info->name); self->midiin_count = 1; } } else { Server_warning(self, "Portmidi warning: Midi Device (%s), not an input device!\n", info->name); self->withPortMidi = 0; } } } else if (self->midi_input >= num_devices) { Server_debug(self, "Midi input device : all!\n"); self->midiin_count = 0; for (i=0; i<num_devices; i++) { const PmDeviceInfo *info = Pm_GetDeviceInfo(i); if (info != NULL) { if (info->input) { pmerr = Pm_OpenInput(&be_data->midiin[self->midiin_count], i, NULL, 100, NULL, NULL); if (pmerr) { Server_warning(self, "Portmidi warning: could not open midi input %d (%s): %s\n", 0, info->name, Pm_GetErrorText(pmerr)); } else { Server_debug(self, "Midi input (%s) opened.\n", info->name); self->midiin_count++; } } } } if (self->midiin_count == 0) self->withPortMidi = 0; } else { Server_warning(self, "Portmidi warning: no input device!\n"); self->withPortMidi = 0; } if (self->midi_output < num_devices) { if (self->midi_output == -1) self->midi_output = Pm_GetDefaultOutputDeviceID(); Server_debug(self, "Midi output device : %d.\n", self->midi_output); const PmDeviceInfo *outinfo = Pm_GetDeviceInfo(self->midi_output); if (outinfo != NULL) { if (outinfo->output) { Pt_Start(1, 0, 0); /* start a timer with millisecond accuracy */ pmerr = Pm_OpenOutput(&be_data->midiout[0], self->midi_output, NULL, 0, NULL, NULL, 1); if (pmerr) { Server_warning(self, "Portmidi warning: could not open midi output %d (%s): %s\n", self->midi_output, outinfo->name, Pm_GetErrorText(pmerr)); self->withPortMidiOut = 0; if (Pt_Started()) Pt_Stop(); } else { Server_debug(self, "Midi output (%s) opened.\n", outinfo->name); self->midiout_count = 1; } } else { Server_warning(self, "Portmidi warning: Midi Device (%s), not an output device!\n", outinfo->name); self->withPortMidiOut = 0; } } } else if (self->midi_output >= num_devices) { Server_debug(self, "Midi output device : all!\n"); self->midiout_count = 0; Pt_Start(1, 0, 0); /* start a timer with millisecond accuracy */ for (i=0; i<num_devices; i++) { const PmDeviceInfo *outinfo = Pm_GetDeviceInfo(i); if (outinfo != NULL) { if (outinfo->output) { pmerr = Pm_OpenOutput(&be_data->midiout[self->midiout_count], i, NULL, 100, NULL, NULL, 1); if (pmerr) { Server_warning(self, "Portmidi warning: could not open midi output %d (%s): %s\n", 0, outinfo->name, Pm_GetErrorText(pmerr)); } else { Server_debug(self, "Midi output (%s) opened.\n", outinfo->name); self->midiout_count++; } } } } if (self->midiout_count == 0) { if (Pt_Started()) Pt_Stop(); self->withPortMidiOut = 0; } } else { Server_warning(self, "Portmidi warning: no output device!\n"); self->withPortMidiOut = 0; } if (self->withPortMidi == 0 && self->withPortMidiOut == 0) { Pm_Terminate(); Server_warning(self, "Portmidi closed.\n"); ret = -1; } } else { Server_warning(self, "Portmidi warning: no midi device found!\nPortmidi closed.\n"); self->withPortMidi = 0; self->withPortMidiOut = 0; Pm_Terminate(); ret = -1; } } if (self->withPortMidi == 1) { self->midi_count = 0; for (i=0; i<self->midiin_count; i++) { Pm_SetFilter(be_data->midiin[i], PM_FILT_ACTIVE | PM_FILT_CLOCK); } } return ret; }
/* * Method: Pt_TimeStarted */ JNIEXPORT jboolean JNICALL Java_jportmidi_JPortMidiApi_Pt_1TimeStarted (JNIEnv *env, jclass c) { return Pt_Started(); }