static void run(LV2_Handle handle, uint32_t n_samples) { SiSco* self = (SiSco*)handle; const uint32_t size = (sizeof(float) * n_samples + 80) * self->n_channels; const uint32_t capacity = self->notify->atom.size; bool capacity_ok = true; /* check if atom-port buffer is large enough to hold * all audio-samples and configuration settings */ if (capacity < size + 216 + self->n_channels * 16) { capacity_ok = false; if (!self->printed_capacity_warning) { fprintf(stderr, "SiSco.lv2 error: LV2 comm-buffersize is insufficient %d/%d bytes.\n", capacity, size + 216 + self->n_channels * 16); self->printed_capacity_warning = true; } } /* prepare forge buffer and initialize atom-sequence */ lv2_atom_forge_set_buffer(&self->forge, (uint8_t*)self->notify, capacity); lv2_atom_forge_sequence_head(&self->forge, &self->frame, 0); /* Send settings to UI */ if (self->send_settings_to_ui && self->ui_active) { self->send_settings_to_ui = false; /* forge container object of type 'ui_state' */ LV2_Atom_Forge_Frame frame; lv2_atom_forge_frame_time(&self->forge, 0); x_forge_object(&self->forge, &frame, 1, self->uris.ui_state); /* forge attributes for 'ui_state' */ lv2_atom_forge_property_head(&self->forge, self->uris.samplerate, 0); lv2_atom_forge_float(&self->forge, capacity_ok ? self->rate : 0); lv2_atom_forge_property_head(&self->forge, self->uris.ui_state_grid, 0); lv2_atom_forge_int(&self->forge, self->ui_grid); lv2_atom_forge_property_head(&self->forge, self->uris.ui_state_trig, 0); lv2_atom_forge_vector(&self->forge, sizeof(float), self->uris.atom_Float, sizeof(struct triggerstate) / sizeof(float), &self->triggerstate); lv2_atom_forge_property_head(&self->forge, self->uris.ui_state_curs, 0); lv2_atom_forge_vector(&self->forge, sizeof(int32_t), self->uris.atom_Int, sizeof(struct cursorstate) / sizeof(int32_t), &self->cursorstate); lv2_atom_forge_property_head(&self->forge, self->uris.ui_state_chn, 0); lv2_atom_forge_vector(&self->forge, sizeof(float), self->uris.atom_Float, self->n_channels * sizeof(struct channelstate) / sizeof(float), self->channelstate); lv2_atom_forge_property_head(&self->forge, self->uris.ui_state_misc, 0); lv2_atom_forge_int(&self->forge, self->ui_misc); /* close-off frame */ lv2_atom_forge_pop(&self->forge, &frame); } /* Process incoming events from GUI */ if (self->control) { LV2_Atom_Event* ev = lv2_atom_sequence_begin(&(self->control)->body); /* for each message from UI... */ while(!lv2_atom_sequence_is_end(&(self->control)->body, (self->control)->atom.size, ev)) { /* .. only look at atom-events.. */ if (ev->body.type == self->uris.atom_Blank || ev->body.type == self->uris.atom_Object) { const LV2_Atom_Object* obj = (LV2_Atom_Object*)&ev->body; /* interpret atom-objects: */ if (obj->body.otype == self->uris.ui_on) { /* UI was activated */ self->ui_active = true; self->send_settings_to_ui = true; } else if (obj->body.otype == self->uris.ui_off) { /* UI was closed */ self->ui_active = false; } else if (obj->body.otype == self->uris.ui_state) { /* UI sends current settings */ const LV2_Atom* grid = NULL; const LV2_Atom* trig = NULL; const LV2_Atom* curs = NULL; const LV2_Atom* misc = NULL; const LV2_Atom* chn = NULL; lv2_atom_object_get(obj, self->uris.ui_state_grid, &grid, self->uris.ui_state_trig, &trig, self->uris.ui_state_curs, &curs, self->uris.ui_state_misc, &misc, self->uris.ui_state_chn, &chn, 0); if (grid && grid->type == self->uris.atom_Int) { self->ui_grid = ((LV2_Atom_Int*)grid)->body; } if (misc && misc->type == self->uris.atom_Int) { self->ui_misc = ((LV2_Atom_Int*)misc)->body; } if (trig && trig->type == self->uris.atom_Vector) { LV2_Atom_Vector *vof = (LV2_Atom_Vector*)LV2_ATOM_BODY(trig); if (vof->atom.type == self->uris.atom_Float) { struct triggerstate *ts = (struct triggerstate *) LV2_ATOM_BODY(&vof->atom); memcpy(&self->triggerstate, ts, sizeof(struct triggerstate)); } } if (curs && curs->type == self->uris.atom_Vector) { LV2_Atom_Vector *vof = (LV2_Atom_Vector*)LV2_ATOM_BODY(curs); if (vof->atom.type == self->uris.atom_Int) { struct cursorstate *cs = (struct cursorstate *) LV2_ATOM_BODY(&vof->atom); memcpy(&self->cursorstate, cs, sizeof(struct cursorstate)); } } if (chn && chn->type == self->uris.atom_Vector) { LV2_Atom_Vector *vof = (LV2_Atom_Vector*)LV2_ATOM_BODY(chn); if (vof->atom.type == self->uris.atom_Float) { struct channelstate *cs = (struct channelstate *) LV2_ATOM_BODY(&vof->atom); memcpy(self->channelstate, cs, self->n_channels * sizeof(struct channelstate)); } } } } ev = lv2_atom_sequence_next(ev); } } /* process audio data */ for (uint32_t c = 0; c < self->n_channels; ++c) { if (self->ui_active && capacity_ok) { /* if UI is active, send raw audio data to UI */ tx_rawaudio(&self->forge, &self->uris, c, n_samples, self->input[c]); } /* if not processing in-place, forward audio */ if (self->input[c] != self->output[c]) { memcpy(self->output[c], self->input[c], sizeof(float) * n_samples); } } /* close off atom-sequence */ lv2_atom_forge_pop(&self->forge, &self->frame); }
/** ==== Run Method ==== */ static void run(LV2_Handle handle, uint32_t n_samples) { EgScope* self = (EgScope*)handle; /* Ensure notify port buffer is large enough to hold all audio-samples and configuration settings. A minimum size was requested in the .ttl file, but check here just to be sure. TODO: Explain these magic numbers. */ const size_t size = (sizeof(float) * n_samples + 64) * self->n_channels; const uint32_t space = self->notify->atom.size; if (space < size + 128) { /* Insufficient space, report error and do nothing. Note that a real-time production plugin mustn't call log functions in run(), but this can be useful for debugging and example purposes. */ lv2_log_error(&self->logger, "Buffer size is insufficient\n"); return; } // Prepare forge buffer and initialize atom-sequence lv2_atom_forge_set_buffer(&self->forge, (uint8_t*)self->notify, space); lv2_atom_forge_sequence_head(&self->forge, &self->frame, 0); /* Send settings to UI The plugin can continue to run while the UI is closed and re-opened. The state and settings of the UI are kept here and transmitted to the UI every time it asks for them or if the user initializes a 'load preset'. */ if (self->send_settings_to_ui && self->ui_active) { self->send_settings_to_ui = false; // Forge container object of type 'ui_state' LV2_Atom_Forge_Frame frame; lv2_atom_forge_frame_time(&self->forge, 0); lv2_atom_forge_object(&self->forge, &frame, 0, self->uris.ui_State); // Add UI state as properties lv2_atom_forge_key(&self->forge, self->uris.ui_spp); lv2_atom_forge_int(&self->forge, self->ui_spp); lv2_atom_forge_key(&self->forge, self->uris.ui_amp); lv2_atom_forge_float(&self->forge, self->ui_amp); lv2_atom_forge_key(&self->forge, self->uris.param_sampleRate); lv2_atom_forge_float(&self->forge, self->rate); lv2_atom_forge_pop(&self->forge, &frame); } // Process incoming events from GUI if (self->control) { const LV2_Atom_Event* ev = lv2_atom_sequence_begin( &(self->control)->body); // For each incoming message... while (!lv2_atom_sequence_is_end( &self->control->body, self->control->atom.size, ev)) { // If the event is an atom:Blank object if (ev->body.type == self->uris.atom_Blank) { const LV2_Atom_Object* obj = (const LV2_Atom_Object*)&ev->body; if (obj->body.otype == self->uris.ui_On) { // If the object is a ui-on, the UI was activated self->ui_active = true; self->send_settings_to_ui = true; } else if (obj->body.otype == self->uris.ui_Off) { // If the object is a ui-off, the UI was closed self->ui_active = false; } else if (obj->body.otype == self->uris.ui_State) { // If the object is a ui-state, it's the current UI settings const LV2_Atom* spp = NULL; const LV2_Atom* amp = NULL; lv2_atom_object_get(obj, self->uris.ui_spp, &spp, self->uris.ui_amp, &, 0); if (spp) { self->ui_spp = ((const LV2_Atom_Int*)spp)->body; } if (amp) { self->ui_amp = ((const LV2_Atom_Float*)amp)->body; } } } ev = lv2_atom_sequence_next(ev); } } // Process audio data for (uint32_t c = 0; c < self->n_channels; ++c) { if (self->ui_active) { // If UI is active, send raw audio data to UI tx_rawaudio(&self->forge, &self->uris, c, n_samples, self->input[c]); } // If not processing audio in-place, forward audio if (self->input[c] != self->output[c]) { memcpy(self->output[c], self->input[c], sizeof(float) * n_samples); } } // Close off sequence lv2_atom_forge_pop(&self->forge, &self->frame); }