1 /*
2  * Copyright (C) 2019-2021 Alexandros Theodotou <alex at zrythm dot org>
3  *
4  * This file is part of Zrythm
5  *
6  * Zrythm is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Zrythm is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with Zrythm.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "audio/audio_region.h"
21 #include "audio/engine.h"
22 #include "audio/group_target_track.h"
23 #include "audio/metronome.h"
24 #include "audio/midi_event.h"
25 #include "audio/midi_file.h"
26 #include "audio/port.h"
27 #include "audio/router.h"
28 #include "audio/sample_processor.h"
29 #include "audio/tempo_track.h"
30 #include "audio/tracklist.h"
31 #include "plugins/plugin.h"
32 #include "plugins/plugin_manager.h"
33 #include "project.h"
34 #include "settings/plugin_settings.h"
35 #include "settings/settings.h"
36 #include "utils/debug.h"
37 #include "utils/dsp.h"
38 #include "utils/error.h"
39 #include "utils/flags.h"
40 #include "utils/io.h"
41 #include "utils/objects.h"
42 
43 #include <glib/gi18n.h>
44 
45 static void
init_common(SampleProcessor * self)46 init_common (
47   SampleProcessor * self)
48 {
49   self->tracklist =
50     tracklist_new (NULL, self);
51   self->midi_events = midi_events_new ();
52 
53   if (!ZRYTHM_TESTING)
54     {
55       char * setting_yaml =
56         g_settings_get_string (
57           S_UI_FILE_BROWSER, "instrument");
58       PluginSetting * setting = NULL;
59       if (strlen (setting_yaml) > 0)
60         {
61           setting =
62             (PluginSetting *)
63             yaml_deserialize (
64               setting_yaml,
65               &plugin_setting_schema);
66           (void) setting;
67         }
68       else
69         {
70           /* pick first instrument found */
71           PluginDescriptor * instrument =
72             plugin_manager_pick_instrument (
73               PLUGIN_MANAGER);
74           if (!instrument)
75             return;
76 
77           setting =
78             plugin_settings_find (
79               S_PLUGIN_SETTINGS, instrument);
80           if (!setting)
81             {
82               setting =
83                 plugin_setting_new_default (
84                   instrument);
85             }
86         }
87 
88       g_return_if_fail (setting);
89       self->instrument_setting =
90         plugin_setting_clone (setting, F_VALIDATE);
91     }
92 }
93 
94 void
sample_processor_init_loaded(SampleProcessor * self,AudioEngine * engine)95 sample_processor_init_loaded (
96   SampleProcessor * self,
97   AudioEngine *     engine)
98 {
99   self->audio_engine = engine;
100   fader_init_loaded (
101     self->fader, NULL, NULL, self);
102 
103   init_common (self);
104 }
105 
106 /**
107  * Initializes a SamplePlayback with a sample to
108  * play back.
109  */
110 SampleProcessor *
sample_processor_new(AudioEngine * engine)111 sample_processor_new (
112   AudioEngine * engine)
113 {
114   SampleProcessor * self =
115     object_new (SampleProcessor);
116   self->audio_engine = engine;
117   self->schema_version =
118     SAMPLE_PROCESSOR_SCHEMA_VERSION;
119 
120   self->fader =
121     fader_new (
122       FADER_TYPE_SAMPLE_PROCESSOR, false,
123       NULL, NULL, self);
124 
125   init_common (self);
126 
127   return self;
128 }
129 
130 /**
131  * Clears the buffers.
132  */
133 void
sample_processor_prepare_process(SampleProcessor * self,const nframes_t nframes)134 sample_processor_prepare_process (
135   SampleProcessor * self,
136   const nframes_t   nframes)
137 {
138   fader_clear_buffers (self->fader);
139 }
140 
141 /**
142  * Removes a SamplePlayback from the array.
143  */
144 void
sample_processor_remove_sample_playback(SampleProcessor * self,SamplePlayback * in_sp)145 sample_processor_remove_sample_playback (
146   SampleProcessor * self,
147   SamplePlayback *  in_sp)
148 {
149   int i, j;
150   SamplePlayback * sp, * next_sp;
151   for (i = 0; i < self->num_current_samples; i++)
152     {
153       sp = &self->current_samples[i];
154       if (in_sp != sp)
155         continue;
156 
157       for (j = i; j < self->num_current_samples - 1;
158            j++)
159         {
160           sp = &self->current_samples[j];
161           next_sp = &self->current_samples[j + 1];
162 
163           sp->buf = next_sp->buf;
164           sp->buf_size = next_sp->buf_size;
165           sp->offset = next_sp->offset;
166           sp->volume = next_sp->volume;
167           sp->start_offset = next_sp->start_offset;
168         }
169       break;
170     }
171   self->num_current_samples--;
172 }
173 
174 
175 /**
176  * Process the samples for the given number of
177  * frames.
178  *
179  * @param cycle_offset The local offset in the
180  *   processing cycle.
181  */
182 void
sample_processor_process(SampleProcessor * self,const nframes_t cycle_offset,const nframes_t nframes)183 sample_processor_process (
184   SampleProcessor * self,
185   const nframes_t   cycle_offset,
186   const nframes_t   nframes)
187 {
188   nframes_t j;
189   nframes_t max_frames;
190   SamplePlayback * sp;
191   g_return_if_fail (
192     self && self->fader &&
193     self->fader->stereo_out &&
194     self->fader->stereo_out->l &&
195     self->fader->stereo_out->l->buf &&
196     self->fader->stereo_out->r &&
197     self->fader->stereo_out->r->buf);
198 
199   z_return_if_fail_cmp (
200     self->num_current_samples, <, 256);
201 
202   float * l = self->fader->stereo_out->l->buf,
203         * r = self->fader->stereo_out->r->buf;
204 
205   /* process the samples in the queue */
206   for (int i = self->num_current_samples - 1;
207        i >= 0; i--)
208     {
209       sp = &self->current_samples[i];
210       z_return_if_fail_cmp (sp->channels, >, 0);
211 
212       /* if sample starts after this cycle (eg,
213        * when counting in for metronome),
214        * update offset and skip processing */
215       if (sp->start_offset >= nframes)
216         {
217           sp->start_offset -= nframes;
218           continue;
219         }
220 
221       /* if sample is already playing */
222       if (sp->offset > 0)
223         {
224           /* fill in the buffer for as many frames
225            * as possible */
226           max_frames =
227             MIN (
228               (nframes_t)
229                 (sp->buf_size - sp->offset),
230               nframes);
231           for (j = 0; j < max_frames; j++)
232             {
233               nframes_t buf_offset =
234                 j + cycle_offset;
235               z_return_if_fail_cmp (
236                 buf_offset, <, nframes)
237               z_return_if_fail_cmp (
238                 sp->offset, <, sp->buf_size);
239               if (sp->channels == 1)
240                 {
241                   l[buf_offset] +=
242                     (*sp->buf)[sp->offset] *
243                     sp->volume;
244                   r[buf_offset] +=
245                     (*sp->buf)[sp->offset++] *
246                     sp->volume;
247                 }
248               else if (sp->channels == 2)
249                 {
250                   l[buf_offset] +=
251                     (*sp->buf)[sp->offset++] *
252                     sp->volume;
253                   r[buf_offset] +=
254                     (*sp->buf)[sp->offset++] *
255                     sp->volume;
256                 }
257             }
258         }
259       /* else if we can start playback in this
260        * cycle */
261       else if (sp->start_offset >= cycle_offset)
262         {
263           z_return_if_fail_cmp (sp->offset, ==, 0);
264           z_return_if_fail_cmp (
265             (cycle_offset + nframes), >=,
266             sp->start_offset);
267 
268           /* fill in the buffer for as many frames
269            * as possible */
270           max_frames =
271             MIN (
272               (nframes_t) sp->buf_size,
273               (cycle_offset + nframes) -
274                 sp->start_offset);
275           for (j = 0; j < max_frames; j++)
276             {
277               nframes_t buf_offset =
278                 j + sp->start_offset;
279               z_return_if_fail_cmp (
280                 buf_offset, <,
281                 cycle_offset + nframes);
282               z_return_if_fail_cmp (
283                 sp->offset, <, sp->buf_size);
284               if (sp->channels == 1)
285                 {
286                   l[buf_offset] +=
287                     (*sp->buf)[sp->offset] *
288                     sp->volume;
289                   r[buf_offset] +=
290                     (*sp->buf)[sp->offset++] *
291                     sp->volume;
292                 }
293               else if (sp->channels == 2)
294                 {
295                   l[buf_offset] +=
296                     (*sp->buf)[sp->offset++] *
297                     sp->volume;
298                   r[buf_offset] +=
299                     (*sp->buf)[sp->offset++] *
300                     sp->volume;
301                 }
302             }
303         }
304 
305       /* if the sample is finished playing, remove
306        * it */
307       if (sp->offset >= sp->buf_size)
308         {
309           sample_processor_remove_sample_playback (
310             self, sp);
311         }
312     }
313 
314   if (self->roll)
315     {
316       midi_events_clear (
317         self->midi_events, F_NOT_QUEUED);
318       for (int i = self->tracklist->num_tracks - 1;
319            i >= 1; i--)
320         {
321           Track * track = self->tracklist->tracks[i];
322 
323           float * audio_data_l = NULL,
324                 * audio_data_r = NULL;
325 
326           track_processor_clear_buffers (
327             track->processor);
328 
329           EngineProcessTimeInfo time_nfo = {
330             .g_start_frames =
331               self->playhead.frames + cycle_offset,
332             .local_offset = cycle_offset,
333             .nframes = nframes, };
334 
335           if (track->type == TRACK_TYPE_AUDIO)
336             {
337               track_processor_process (
338                 track->processor, &time_nfo);
339 
340               audio_data_l =
341                 track->processor->stereo_out->l->buf;
342               audio_data_r =
343                 track->processor->stereo_out->l->buf;
344             }
345           else if (track->type == TRACK_TYPE_MIDI)
346             {
347               track_processor_process (
348                 track->processor, &time_nfo);
349               midi_events_append (
350                 track->processor->midi_out->
351                   midi_events,
352                 self->midi_events,
353                 cycle_offset, nframes,
354                 F_NOT_QUEUED);
355             }
356           else if (
357             track->type == TRACK_TYPE_INSTRUMENT)
358             {
359               if (!track->channel->instrument)
360                 return;
361 
362               plugin_prepare_process (
363                 track->channel->instrument);
364               midi_events_append (
365                 self->midi_events,
366                 track->channel->instrument->
367                   midi_in_port->midi_events,
368                 cycle_offset, nframes,
369                 F_NOT_QUEUED);
370               plugin_process (
371                 track->channel->instrument,
372                 self->playhead.frames + cycle_offset,
373                 cycle_offset, nframes);
374               audio_data_l =
375                 track->channel->instrument->l_out->
376                   buf;
377               audio_data_r =
378                 track->channel->instrument->r_out->
379                   buf;
380             }
381 
382           if (audio_data_l && audio_data_r)
383             {
384               dsp_mix2 (
385                 &l[cycle_offset],
386                 &audio_data_l[cycle_offset],
387                 1.f, self->fader->amp->control,
388                 nframes);
389               dsp_mix2 (
390                 &r[cycle_offset],
391                 &audio_data_r[cycle_offset],
392                 1.f, self->fader->amp->control,
393                 nframes);
394             }
395         }
396     }
397 
398   position_add_frames (&self->playhead, nframes);
399 
400   /* stop rolling if no more material */
401   if (position_is_after (
402         &self->playhead, &self->file_end_pos))
403     self->roll = false;
404 }
405 
406 /**
407  * Queues a metronomem tick at the given offset.
408  *
409  * Used for countin.
410  */
411 void
sample_processor_queue_metronome_countin(SampleProcessor * self)412 sample_processor_queue_metronome_countin (
413   SampleProcessor * self)
414 {
415   PrerollCountBars bars =
416     g_settings_get_enum (
417       S_TRANSPORT, "metronome-countin");
418   int num_bars =
419     transport_preroll_count_bars_enum_to_int (bars);
420   int beats_per_bar =
421     tempo_track_get_beats_per_bar (P_TEMPO_TRACK);
422   int num_beats = beats_per_bar * num_bars;
423 
424   double frames_per_bar =
425     AUDIO_ENGINE->frames_per_tick *
426     (double) TRANSPORT->ticks_per_bar;
427   for (int i = 0; i < num_bars; i++)
428     {
429       long offset =
430         (long) ((double) i * frames_per_bar);
431       SamplePlayback * sp =
432         &self->current_samples[
433           self->num_current_samples];
434       sample_playback_init (
435         sp, &METRONOME->emphasis,
436         METRONOME->emphasis_size,
437         METRONOME->emphasis_channels,
438         0.1f * METRONOME->volume, offset);
439       self->num_current_samples++;
440     }
441 
442   double frames_per_beat =
443     AUDIO_ENGINE->frames_per_tick *
444     (double) TRANSPORT->ticks_per_beat;
445   for (int i = 0; i < num_beats; i++)
446     {
447       if (i % beats_per_bar == 0)
448         continue;
449 
450       long offset =
451         (long) ((double) i * frames_per_beat);
452       SamplePlayback * sp =
453         &self->current_samples[
454           self->num_current_samples];
455       sample_playback_init (
456         sp, &METRONOME->normal,
457         METRONOME->normal_size,
458         METRONOME->normal_channels,
459         0.1f * METRONOME->volume, offset);
460       self->num_current_samples++;
461     }
462 }
463 
464 /**
465  * Queues a metronomem tick at the given local
466  * offset.
467  *
468  * Realtime function.
469  */
470 void
sample_processor_queue_metronome(SampleProcessor * self,MetronomeType type,nframes_t offset)471 sample_processor_queue_metronome (
472   SampleProcessor * self,
473   MetronomeType     type,
474   nframes_t         offset)
475 {
476   g_return_if_fail (
477     METRONOME->emphasis && METRONOME->normal);
478 
479 #if 0
480   Position pos;
481   position_set_to_pos (&pos, PLAYHEAD);
482   position_add_frames (&pos, offset);
483   char metronome_pos_str[60];
484   position_to_string (&pos, metronome_pos_str);
485   g_message (
486     "%s metronome queued at %s (loffset %d)",
487     (type == METRONOME_TYPE_EMPHASIS) ?
488       "emphasis" : "normal",
489     metronome_pos_str, offset);
490 #endif
491 
492   SamplePlayback * sp =
493     &self->current_samples[
494       self->num_current_samples];
495 
496   g_return_if_fail (
497     offset < AUDIO_ENGINE->block_length);
498 
499   /*g_message ("queuing %u", offset);*/
500   if (type == METRONOME_TYPE_EMPHASIS)
501     {
502       sample_playback_init (
503         sp, &METRONOME->emphasis,
504         METRONOME->emphasis_size,
505         METRONOME->emphasis_channels,
506         0.1f * METRONOME->volume, offset);
507     }
508   else if (type == METRONOME_TYPE_NORMAL)
509     {
510       sample_playback_init (
511         sp, &METRONOME->normal,
512         METRONOME->normal_size,
513         METRONOME->normal_channels,
514         0.1f * METRONOME->volume, offset);
515     }
516 
517   self->num_current_samples++;
518 }
519 
520 /**
521  * Adds a sample to play to the queue from a file
522  * path.
523  */
524 void
sample_processor_queue_sample_from_file(SampleProcessor * self,const char * path)525 sample_processor_queue_sample_from_file (
526   SampleProcessor * self,
527   const char *      path)
528 {
529   /* TODO */
530 }
531 
532 /**
533  * Adds a file (audio or MIDI) to the queue.
534  */
535 void
sample_processor_queue_file(SampleProcessor * self,const SupportedFile * file)536 sample_processor_queue_file (
537   SampleProcessor *     self,
538   const SupportedFile * file)
539 {
540   EngineState state;
541   engine_wait_for_pause (
542     AUDIO_ENGINE, &state, false);
543 
544   /* clear tracks */
545   for (int i = self->tracklist->num_tracks - 1;
546        i >= 0; i--)
547     {
548       Track * track = self->tracklist->tracks[i];
549 
550       /* remove state dir if instrument */
551       if (track->type == TRACK_TYPE_INSTRUMENT)
552         {
553           char * state_dir =
554             plugin_get_abs_state_dir (
555               track->channel->instrument,
556               F_NOT_BACKUP);
557           if (state_dir)
558             {
559               io_rmdir (state_dir, F_FORCE);
560               g_free (state_dir);
561             }
562         }
563 
564       tracklist_remove_track (
565         self->tracklist, track, true,
566         F_FREE, F_NO_PUBLISH_EVENTS,
567         F_NO_RECALC_GRAPH);
568     }
569 
570   Position start_pos;
571   position_set_to_bar (&start_pos, 1);
572   position_set_to_bar (&self->file_end_pos, 1);
573 
574   /* create master track */
575   g_debug ("creating master track...");
576   Track * track =
577     track_new (
578       TRACK_TYPE_MASTER, self->tracklist->num_tracks,
579       "Sample Processor Master", F_WITHOUT_LANE);
580   self->tracklist->master_track = track;
581   tracklist_insert_track (
582     self->tracklist, track, track->pos,
583     F_NO_PUBLISH_EVENTS, F_NO_RECALC_GRAPH);
584 
585   if (supported_file_type_is_audio (file->type))
586     {
587       g_debug ("creating audio track...");
588       track =
589         track_new (
590           TRACK_TYPE_AUDIO,
591           self->tracklist->num_tracks,
592           "Sample processor audio",
593           F_WITH_LANE);
594       tracklist_insert_track (
595         self->tracklist, track, track->pos,
596         F_NO_PUBLISH_EVENTS, F_NO_RECALC_GRAPH);
597 
598       /* create an audio region & add to
599        * track */
600       ZRegion * ar =
601         audio_region_new (
602           -1, file->abs_path, false,
603           NULL, 0, NULL, 0, 0,
604           &start_pos, 0, 0, 0);
605       track_add_region (
606         track, ar, NULL, 0, F_GEN_NAME,
607         F_NO_PUBLISH_EVENTS);
608 
609       ArrangerObject * obj =  (ArrangerObject *) ar;
610       position_set_to_pos (
611         &self->file_end_pos, &obj->end_pos);
612     }
613   else if (supported_file_type_is_midi (
614              file->type) &&
615            self->instrument_setting)
616     {
617       /* create an instrument track */
618       g_debug ("creating instrument track...");
619       Track * instrument_track =
620         track_new (
621           TRACK_TYPE_INSTRUMENT,
622           self->tracklist->num_tracks,
623           "Sample processor instrument",
624           F_WITH_LANE);
625       tracklist_insert_track (
626         self->tracklist, instrument_track,
627         instrument_track->pos,
628         F_NO_PUBLISH_EVENTS, F_NO_RECALC_GRAPH);
629       GError * err = NULL;
630       Plugin * pl =
631         plugin_new_from_setting (
632           self->instrument_setting,
633           track_get_name_hash (instrument_track),
634           PLUGIN_SLOT_INSTRUMENT, -1, &err);
635       if (!pl)
636         {
637           HANDLE_ERROR (
638             err,
639             _("Failed to create plugin %s"),
640             self->instrument_setting->descr->name);
641           return;
642         }
643       int ret =
644         plugin_instantiate (pl, NULL, &err);
645       if (ret != 0)
646         {
647           HANDLE_ERROR (
648             err,
649             _("Failed to instantiate plugin %s"),
650             self->instrument_setting->descr->name);
651           return;
652         }
653       g_return_if_fail (
654         plugin_activate (pl, F_ACTIVATE) == 0);
655       g_return_if_fail (pl->midi_in_port);
656       g_return_if_fail (pl->l_out);
657       g_return_if_fail (pl->r_out);
658       channel_add_plugin (
659         instrument_track->channel,
660         PLUGIN_SLOT_INSTRUMENT,
661         pl->id.slot, pl, F_NO_CONFIRM,
662         F_NOT_MOVING_PLUGIN, F_GEN_AUTOMATABLES,
663         F_NO_RECALC_GRAPH, F_NO_PUBLISH_EVENTS);
664 
665       int num_tracks =
666         midi_file_get_num_tracks (
667           file->abs_path, true);
668       g_debug (
669         "creating %d MIDI tracks...", num_tracks);
670       for (int i = 0; i < num_tracks; i++)
671         {
672           char name[600];
673           sprintf (
674             name, "Sample processor MIDI %d", i);
675           track =
676             track_new (
677               TRACK_TYPE_MIDI,
678               self->tracklist->num_tracks, name,
679               F_WITH_LANE);
680           tracklist_insert_track (
681             self->tracklist, track, track->pos,
682             F_NO_PUBLISH_EVENTS, F_NO_RECALC_GRAPH);
683 
684           /* route track to instrument */
685           group_target_track_add_child (
686             instrument_track,
687             track_get_name_hash (track),
688             F_CONNECT, F_NO_RECALC_GRAPH,
689             F_NO_PUBLISH_EVENTS);
690 
691           /* create a MIDI region from the MIDI
692            * file & add to track */
693           ZRegion * mr =
694             midi_region_new_from_midi_file (
695               &start_pos, file->abs_path,
696               track_get_name_hash (track), 0, 0, i);
697           if (mr)
698             {
699               track_add_region (
700                 track, mr, NULL, 0,
701                 /* name could already be generated
702                  * based
703                  * on the track name (if any) in
704                  * the MIDI file */
705                 mr->name ?
706                   F_NO_GEN_NAME : F_GEN_NAME,
707                 F_NO_PUBLISH_EVENTS);
708 
709               ArrangerObject * obj =
710                 (ArrangerObject *) mr;
711               if (position_is_after (
712                     &obj->end_pos,
713                     &self->file_end_pos))
714                 {
715                   position_set_to_pos (
716                     &self->file_end_pos,
717                     &obj->end_pos);
718                 }
719             }
720           else
721             {
722               g_message (
723                 "Failed to create MIDI region from "
724                 "file %s",
725                 file->abs_path);
726             }
727         }
728     }
729 
730   self->roll = true;
731   position_set_to_bar (&self->playhead, 1);
732 
733   /* add some room to end pos */
734   position_add_bars (&self->file_end_pos, 1);
735 
736   router_recalc_graph (ROUTER, F_NOT_SOFT);
737 
738   engine_resume (AUDIO_ENGINE, &state);
739 }
740 
741 /**
742  * Stops playback of files (auditioning).
743  */
744 void
sample_processor_stop_file_playback(SampleProcessor * self)745 sample_processor_stop_file_playback (
746   SampleProcessor *     self)
747 {
748   EngineState state;
749   engine_wait_for_pause (
750     AUDIO_ENGINE, &state, false);
751 
752   self->roll = false;
753   position_set_to_bar (&self->playhead, 1);
754 
755   engine_resume (AUDIO_ENGINE, &state);
756 }
757 
758 void
sample_processor_disconnect(SampleProcessor * self)759 sample_processor_disconnect (
760   SampleProcessor * self)
761 {
762   fader_disconnect_all (self->fader);
763 }
764 
765 SampleProcessor *
sample_processor_clone(const SampleProcessor * src)766 sample_processor_clone (
767   const SampleProcessor * src)
768 {
769   SampleProcessor * self =
770     object_new (SampleProcessor);
771   self->schema_version =
772     SAMPLE_PROCESSOR_SCHEMA_VERSION;
773 
774   self->fader = fader_clone (src->fader);
775 
776   return self;
777 }
778 
779 void
sample_processor_free(SampleProcessor * self)780 sample_processor_free (
781   SampleProcessor * self)
782 {
783   if (self == SAMPLE_PROCESSOR)
784     sample_processor_disconnect (self);
785 
786   object_free_w_func_and_null (
787     tracklist_free, self->tracklist);
788   object_free_w_func_and_null (
789     fader_free, self->fader);
790 
791   object_zero_and_free (self);
792 }
793