Initial attempt at timestretching implementation.
[calfbox.git] / seq.h
blob1b8f5663f747e2deb6d2980e0ebb97f7b0fc5e82
1 /*
2 Calf Box, an open source musical instrument.
3 Copyright (C) 2010-2011 Krzysztof Foltman
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #ifndef CBOX_SEQ_H
20 #define CBOX_SEQ_H
22 #include <stdint.h>
24 #include "midi.h"
25 #include "mididest.h"
27 struct cbox_engine;
28 struct cbox_midi_pattern;
29 struct cbox_track;
30 struct cbox_song;
32 /////////////////////////////////////////////////////////////////////////////////////////////////////
34 struct cbox_midi_playback_active_notes
36 uint16_t channels_active;
37 uint32_t notes[16][4]; // 0..127
40 extern void cbox_midi_playback_active_notes_init(struct cbox_midi_playback_active_notes *notes);
41 extern void cbox_midi_playback_active_notes_copy(struct cbox_midi_playback_active_notes *dest, const struct cbox_midi_playback_active_notes *src);
42 extern void cbox_midi_playback_active_notes_clear(struct cbox_midi_playback_active_notes *notes);
43 extern int cbox_midi_playback_active_notes_release(struct cbox_midi_playback_active_notes *notes, struct cbox_midi_buffer *buf);
45 /////////////////////////////////////////////////////////////////////////////////////////////////////
47 struct cbox_midi_pattern_playback
49 struct cbox_midi_event *events;
50 int event_count;
53 extern struct cbox_midi_pattern_playback *cbox_midi_pattern_playback_new(struct cbox_midi_pattern *pattern);
54 extern void cbox_midi_pattern_playback_destroy(struct cbox_midi_pattern_playback *mppb);
56 /////////////////////////////////////////////////////////////////////////////////////////////////////
58 struct cbox_midi_clip_playback
60 struct cbox_midi_pattern_playback *pattern;
61 struct cbox_master *master;
63 int pos;
64 int rel_time_samples;
65 // [start, end) of the pattern slice
66 int start_time_samples, end_time_samples;
67 int item_start_ppqn, min_time_ppqn;
68 int offset_ppqn;
69 struct cbox_midi_playback_active_notes *active_notes;
72 extern void cbox_midi_clip_playback_init(struct cbox_midi_clip_playback *pb, struct cbox_midi_playback_active_notes *active_notes, struct cbox_master *master);
73 extern void cbox_midi_clip_playback_render(struct cbox_midi_clip_playback *pb, struct cbox_midi_buffer *buf, int offset, int nsamples);
74 extern void cbox_midi_clip_playback_seek_ppqn(struct cbox_midi_clip_playback *pb, int time_ppqn, int skip_this_pos);
75 extern void cbox_midi_clip_playback_seek_samples(struct cbox_midi_clip_playback *pb, int time_samples);
76 extern void cbox_midi_clip_playback_set_pattern(struct cbox_midi_clip_playback *pb, struct cbox_midi_pattern_playback *pattern, int start_time_samples, int end_time_samples, int item_start_ppqn, int offset_ppqn);
78 /////////////////////////////////////////////////////////////////////////////////////////////////////
80 // all times in this structure are in PPQN
81 struct cbox_track_playback_item
83 uint32_t time;
84 struct cbox_midi_pattern_playback *pattern;
85 uint32_t offset;
86 uint32_t length;
87 // in future, it should also contain a pre-calculated list of notes to release
90 struct cbox_track_playback
92 struct cbox_track *track; // used as identification only
93 struct cbox_track_playback_item *items;
94 struct cbox_master *master;
95 int items_count;
96 int pos;
97 struct cbox_midi_buffer output_buffer;
98 struct cbox_midi_clip_playback playback;
99 struct cbox_midi_playback_active_notes active_notes;
100 struct cbox_midi_merger *external_merger;
101 struct cbox_song_playback *spb;
102 struct cbox_track_playback *old_state;
103 gboolean state_copied;
106 extern struct cbox_track_playback *cbox_track_playback_new_from_track(struct cbox_track *track, struct cbox_master *master, struct cbox_song_playback *spb, struct cbox_track_playback *old_state);
107 extern void cbox_track_playback_render(struct cbox_track_playback *pb, int offset, int nsamples);
108 extern void cbox_track_playback_seek_ppqn(struct cbox_track_playback *pb, int time_ppqn, int min_time_ppqn);
109 extern void cbox_track_playback_seek_samples(struct cbox_track_playback *pb, int time_samples);
110 extern void cbox_track_playback_start_item(struct cbox_track_playback *pb, int time, int is_ppqn, int skip_this_pos);
111 extern void cbox_track_playback_destroy(struct cbox_track_playback *pb);
113 /////////////////////////////////////////////////////////////////////////////////////////////////////
115 struct cbox_tempo_map_item
117 uint32_t time_ppqn;
118 uint32_t time_samples;
119 double tempo;
120 int timesig_nom, timesig_denom;
121 // should also have a bar/beat position to make things easier
124 /////////////////////////////////////////////////////////////////////////////////////////////////////
126 struct cbox_adhoc_pattern
128 struct cbox_adhoc_pattern *next;
130 struct cbox_master *master;
131 struct cbox_midi_pattern *pattern;
132 struct cbox_midi_pattern_playback *pattern_playback;
134 struct cbox_midi_playback_active_notes active_notes;
135 struct cbox_midi_clip_playback playback;
137 struct cbox_midi_buffer output_buffer;
138 int id;
139 gboolean completed;
142 extern struct cbox_adhoc_pattern *cbox_adhoc_pattern_new(struct cbox_engine *engine, int id, struct cbox_midi_pattern *pattern);
143 extern void cbox_adhoc_pattern_render(struct cbox_adhoc_pattern *adp, int offset, int nsamples);
144 extern void cbox_adhoc_pattern_destroy(struct cbox_adhoc_pattern *ap);
146 /////////////////////////////////////////////////////////////////////////////////////////////////////
148 struct cbox_song_playback
150 struct cbox_master *master;
151 struct cbox_song *song; // for identification only
152 struct cbox_track_playback **tracks;
153 int track_count;
154 struct cbox_tempo_map_item *tempo_map_items;
155 int tempo_map_item_count;
156 int tempo_map_pos;
157 uint32_t song_pos_samples, song_pos_ppqn, min_time_ppqn;
158 uint32_t loop_start_ppqn, loop_end_ppqn;
159 GHashTable *pattern_map;
160 struct cbox_midi_merger track_merger;
161 struct cbox_engine *engine;
164 extern struct cbox_song_playback *cbox_song_playback_new(struct cbox_song *song, struct cbox_master *master, struct cbox_engine *engine, struct cbox_song_playback *old_state);
165 extern void cbox_song_playback_render(struct cbox_song_playback *spb, struct cbox_midi_buffer *output, int nsamples);
166 extern int cbox_song_playback_active_notes_release(struct cbox_song_playback *spb, struct cbox_midi_buffer *buf);
167 extern void cbox_song_playback_seek_ppqn(struct cbox_song_playback *spb, int time_ppqn, int skip_this_pos);
168 extern void cbox_song_playback_seek_samples(struct cbox_song_playback *spb, int time_samples);
169 extern int cbox_song_playback_tmi_from_ppqn(struct cbox_song_playback *spb, int time_ppqn);
170 extern int cbox_song_playback_tmi_from_samples(struct cbox_song_playback *spb, int time_samples);
171 struct cbox_midi_pattern_playback *cbox_song_playback_get_pattern(struct cbox_song_playback *spb, struct cbox_midi_pattern *pattern);
172 extern void cbox_song_playback_apply_old_state(struct cbox_song_playback *spb);
173 extern void cbox_song_playback_destroy(struct cbox_song_playback *spb);
175 #endif