1 /*
2  * Copyright (C) 2019-2020 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 /**
21  * \file
22  *
23  * Object to hold information for the Tempo track.
24  */
25 
26 #ifndef __AUDIO_TEMPO_TRACK_H__
27 #define __AUDIO_TEMPO_TRACK_H__
28 
29 #include <stdint.h>
30 
31 #include "audio/track.h"
32 #include "utils/types.h"
33 
34 /**
35  * @addtogroup audio
36  *
37  * @{
38  */
39 
40 #define TEMPO_TRACK_MAX_BPM 420.f
41 #define TEMPO_TRACK_MIN_BPM 40.f
42 #define TEMPO_TRACK_DEFAULT_BPM 140.f
43 #define TEMPO_TRACK_DEFAULT_BEATS_PER_BAR 4
44 #define TEMPO_TRACK_MIN_BEATS_PER_BAR 1
45 #define TEMPO_TRACK_MAX_BEATS_PER_BAR 16
46 #define TEMPO_TRACK_DEFAULT_BEAT_UNIT BEAT_UNIT_4
47 #define TEMPO_TRACK_MIN_BEAT_UNIT BEAT_UNIT_2
48 #define TEMPO_TRACK_MAX_BEAT_UNIT BEAT_UNIT_16
49 
50 #define P_TEMPO_TRACK (TRACKLIST->tempo_track)
51 
52 /**
53  * Beat unit.
54  */
55 typedef enum BeatUnit
56 {
57   BEAT_UNIT_2,
58   BEAT_UNIT_4,
59   BEAT_UNIT_8,
60   BEAT_UNIT_16
61 } BeatUnit;
62 
63 static const cyaml_strval_t
64 beat_unit_strings[] =
65 {
66   { "2",      BEAT_UNIT_2    },
67   { "4",      BEAT_UNIT_4    },
68   { "8",      BEAT_UNIT_8    },
69   { "16",     BEAT_UNIT_16   },
70 };
71 
72 /**
73  * Creates the default tempo track.
74  */
75 Track *
76 tempo_track_default (
77   int   track_pos);
78 
79 /**
80  * Inits the tempo track.
81  */
82 void
83 tempo_track_init (
84   Track * track);
85 
86 /**
87  * Removes all objects from the tempo track.
88  *
89  * Mainly used in testing.
90  */
91 void
92 tempo_track_clear (
93   Track * self);
94 
95 /**
96  * Returns the BPM at the given pos.
97  */
98 bpm_t
99 tempo_track_get_bpm_at_pos (
100   Track *    track,
101   Position * pos);
102 
103 /**
104  * Returns the current BPM.
105  */
106 bpm_t
107 tempo_track_get_current_bpm (
108   Track * self);
109 
110 const char *
111 tempo_track_get_current_bpm_as_str (
112   void * self);
113 
114 /**
115  * Sets the BPM.
116  *
117  * @param update_snap_points Whether to update the
118  *   snap points.
119  * @param stretch_audio_region Whether to stretch
120  *   audio regions. This should only be true when
121  *   the BPM change is final.
122  * @param start_bpm The BPM at the start of the
123  *   action, if not temporary.
124  */
125 void
126 tempo_track_set_bpm (
127   Track * self,
128   bpm_t   bpm,
129   bpm_t   start_bpm,
130   bool    temporary,
131   bool    fire_events);
132 
133 void
134 tempo_track_set_bpm_from_str (
135   void *       _self,
136   const char * str);
137 
138 int
139 tempo_track_beat_unit_enum_to_int (
140   BeatUnit ebeat_unit);
141 
142 void
143 tempo_track_set_beat_unit_from_enum (
144   Track *  self,
145   BeatUnit ebeat_unit);
146 
147 BeatUnit
148 tempo_track_get_beat_unit_enum (
149   Track * self);
150 
151 BeatUnit
152 tempo_track_beat_unit_to_enum (
153   int beat_unit);
154 
155 void
156 tempo_track_set_beat_unit (
157   Track * self,
158   int     beat_unit);
159 
160 /**
161  * Updates beat unit and anything depending on it.
162  */
163 void
164 tempo_track_set_beats_per_bar (
165   Track *     self,
166   int         beats_per_bar);
167 
168 int
169 tempo_track_get_beats_per_bar (
170   Track * self);
171 
172 int
173 tempo_track_get_beat_unit (
174   Track * self);
175 
176 /**
177  * @}
178  */
179 
180 #endif
181