int jack_srate_cb(jack_nframes_t nframes, void *arg) { Server *s = (Server *) arg; s->samplingRate = (double) nframes; Server_debug(s, "The sample rate is now %lu.\n", (unsigned long) nframes); return 0; }
int jack_bufsize_cb(jack_nframes_t nframes, void *arg) { Server *s = (Server *) arg; s->bufferSize = (int) nframes; Server_debug(s, "The buffer size is now %lu.\n", (unsigned long) nframes); return 0; }
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; }
int Server_jack_init(Server *self) { char client_name[32]; char name[16]; const char *server_name = "server"; jack_options_t options = JackNullOption; jack_status_t status; int sampleRate = 0; int bufferSize = 0; int nchnls = 0; int total_nchnls = 0; int index = 0; int ret = 0; assert(self->audio_be_data == NULL); PyoJackBackendData *be_data = (PyoJackBackendData *) malloc(sizeof(PyoJackBackendData *)); self->audio_be_data = (void *) be_data; be_data->jack_in_ports = (jack_port_t **) calloc(self->ichnls + self->input_offset, sizeof(jack_port_t *)); be_data->jack_out_ports = (jack_port_t **) calloc(self->nchnls + self->output_offset, sizeof(jack_port_t *)); strncpy(client_name,self->serverName, 32); be_data->jack_client = jack_client_open(client_name, options, &status, server_name); if (be_data->jack_client == NULL) { Server_error(self, "Jack error: Unable to create JACK client\n"); if (status & JackServerFailed) { Server_debug(self, "Jack error: jack_client_open() failed, " "status = 0x%2.0x\n", status); } return -1; } if (status & JackServerStarted) { Server_warning(self, "JACK server started.\n"); } if (strcmp(self->serverName, jack_get_client_name(be_data->jack_client)) ) { strcpy(self->serverName, jack_get_client_name(be_data->jack_client)); Server_warning(self, "Jack name `%s' assigned\n", self->serverName); } sampleRate = jack_get_sample_rate (be_data->jack_client); if (sampleRate != self->samplingRate) { self->samplingRate = (double)sampleRate; Server_warning(self, "Sample rate set to Jack engine sample rate: %" PRIu32 "\n", sampleRate); } else { Server_debug(self, "Jack engine sample rate: %" PRIu32 "\n", sampleRate); } if (sampleRate <= 0) { Server_error(self, "Invalid Jack engine sample rate."); jack_client_close(be_data->jack_client); return -1; } bufferSize = jack_get_buffer_size(be_data->jack_client); if (bufferSize != self->bufferSize) { self->bufferSize = bufferSize; Server_warning(self, "Buffer size set to Jack engine buffer size: %" PRIu32 "\n", bufferSize); } else { Server_debug(self, "Jack engine buffer size: %" PRIu32 "\n", bufferSize); } nchnls = total_nchnls = self->ichnls + self->input_offset; while (nchnls-- > 0) { index = total_nchnls - nchnls - 1; ret = sprintf(name, "input_%i", index + 1); if (ret > 0) { be_data->jack_in_ports[index] = jack_port_register(be_data->jack_client, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0); } if ((be_data->jack_in_ports[index] == NULL)) { Server_error(self, "Jack: no more JACK input ports available\n"); return -1; } } nchnls = total_nchnls = self->nchnls + self->output_offset; while (nchnls-- > 0) { index = total_nchnls - nchnls - 1; ret = sprintf(name, "output_%i", index + 1); if (ret > 0) { be_data->jack_out_ports[index] = jack_port_register(be_data->jack_client, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0); } if ((be_data->jack_out_ports[index] == NULL)) { Server_error(self, "Jack: no more JACK output ports available\n"); return -1; } } jack_set_error_function(jack_error_cb); jack_set_sample_rate_callback(be_data->jack_client, jack_srate_cb, (void *) self); jack_on_shutdown(be_data->jack_client, jack_shutdown_cb, (void *) self); jack_set_buffer_size_callback(be_data->jack_client, jack_bufsize_cb, (void *) self); return 0; }