1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
//
// AudioSettings.c - MrsWatson
// Created by Nik Reiman on 1/4/12.
// Copyright (c) 2012 Teragon Audio. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "audio/AudioSettings.h"
#include "logging/EventLogger.h"
AudioSettings audioSettingsInstance = NULL;
void initAudioSettings(void)
{
if (audioSettingsInstance != NULL) {
freeAudioSettings();
}
audioSettingsInstance = malloc(sizeof(AudioSettingsMembers));
audioSettingsInstance->sampleRate = DEFAULT_SAMPLE_RATE;
audioSettingsInstance->numChannels = DEFAULT_NUM_CHANNELS;
audioSettingsInstance->blocksize = DEFAULT_BLOCKSIZE;
audioSettingsInstance->tempo = DEFAULT_TEMPO;
audioSettingsInstance->timeSignatureBeatsPerMeasure = DEFAULT_TIMESIG_BEATS_PER_MEASURE;
audioSettingsInstance->timeSignatureNoteValue = DEFAULT_TIMESIG_NOTE_VALUE;
}
static AudioSettings _getAudioSettings(void)
{
if (audioSettingsInstance == NULL) {
initAudioSettings();
}
return audioSettingsInstance;
}
SampleRate getSampleRate(void)
{
return _getAudioSettings()->sampleRate;
}
ChannelCount getNumChannels(void)
{
return _getAudioSettings()->numChannels;
}
SampleCount getBlocksize(void)
{
return _getAudioSettings()->blocksize;
}
Tempo getTempo(void)
{
return _getAudioSettings()->tempo;
}
unsigned short getTimeSignatureBeatsPerMeasure(void)
{
return _getAudioSettings()->timeSignatureBeatsPerMeasure;
}
unsigned short getTimeSignatureNoteValue(void)
{
return _getAudioSettings()->timeSignatureNoteValue;
}
boolByte setSampleRate(const SampleRate sampleRate)
{
if (sampleRate <= 0.0f) {
logError("Can't set sample rate to %f", sampleRate);
return false;
}
logInfo("Setting sample rate to %gHz", sampleRate);
_getAudioSettings()->sampleRate = sampleRate;
return true;
}
boolByte setNumChannels(const ChannelCount numChannels)
{
if (numChannels <= 0) {
logError("Can't set channel count to %d", numChannels);
return false;
}
logInfo("Setting %d channels", numChannels);
_getAudioSettings()->numChannels = numChannels;
return true;
}
boolByte setBlocksize(const SampleCount blocksize)
{
if (blocksize <= 0) {
logError("Can't set invalid blocksize %d", blocksize);
return false;
}
logInfo("Setting blocksize to %ld", blocksize);
_getAudioSettings()->blocksize = blocksize;
return true;
}
boolByte setTempo(const Tempo tempo)
{
if (tempo <= 0.0f) {
logError("Cannot set tempo to %f", tempo);
return false;
}
//here
logInfo("NOT more Setting tempo to %f", tempo);
_getAudioSettings()->tempo = tempo;
return true;
}
void setTempoFromMidiBytes(const byte *bytes)
{
double tempo;
unsigned long beatLengthInMicroseconds = 0;
printf("inside midi bytes function");
if (bytes != NULL) {
float tempotest = getTempo();
//seem like a good way to test it? yep
printf ("THIS WAS THE TEMPO: %f", tempotest);
if (!getTempo()){ //something like this? not sure how to tell if a struct is empty it's integer
logInfo("tempo was empty so setting to default value");
beatLengthInMicroseconds = (unsigned long)(0x00000000 | (bytes[0] << 16) | (bytes[1] << 8) | (bytes[2]));
// Convert beats / microseconds -> beats / minutes
tempo = (1000000.0 / (double)beatLengthInMicroseconds) * 60.0;
tempo = 120; //(1000000.0 / (double)2000) * 60.0;
//i guess this function just not called at all yeah but where is tempo Set then? default of some kind
//almost like this file isn't getting compiled. above I set tempo to 120, not 100 it might be not calling this function because bytes == NULL
//something like this? yes
setTempo((float)tempo);
}else{
logInfo("Using tempo from command line args");
}// so midi doesn't have tempo setting i guess t's setting to 100 now, it is supplied from cli? no
}else{
logInfo("bytes == NULL");
}// so midi doesn't have tempo setting i guess t's setting to 100 now, it is supplied from cli? no
}
boolByte setTimeSignatureBeatsPerMeasure(const unsigned short beatsPerMeasure)
{
// Bit of an easter egg :)
if (beatsPerMeasure < 2 || beatsPerMeasure > 12) {
logInfo("Freaky time signature, but whatever you say...");
}
if (beatsPerMeasure <= 0) {
logError("Ignoring attempt to set time signature numerator to %d", beatsPerMeasure);
return false;
}
_getAudioSettings()->timeSignatureBeatsPerMeasure = beatsPerMeasure;
return true;
}
boolByte setTimeSignatureNoteValue(const unsigned short noteValue)
{
// Bit of an easter egg :)
if (!(noteValue == 2 || noteValue == 4 || noteValue == 8 || noteValue == 16) || noteValue < 2 || noteValue > 16) {
logInfo("Interesting time signature you've chosen. I'm sure this piece is going to sound great...");
}
if (noteValue <= 0) {
logError("Ignoring attempt to set time signature denominator to %d", noteValue);
return false;
}
_getAudioSettings()->timeSignatureNoteValue = noteValue;
return true;
}
boolByte setTimeSignatureFromString(const CharString signature)
{
char *slash = NULL;
unsigned short numerator = 0;
unsigned short denominator = 0;
if (!charStringIsEmpty(signature)) {
slash = strchr(signature->data, '/');
if (slash != NULL) {
*slash = '\0';
numerator = (unsigned short)strtod(signature->data, NULL);
denominator = (unsigned short)strtod(slash + 1, NULL);
if (numerator > 0 && denominator > 0) {
return (boolByte)(setTimeSignatureBeatsPerMeasure(numerator) &&
setTimeSignatureNoteValue(denominator));
}
}
}
return false;
}
boolByte setTimeSignatureFromMidiBytes(const byte *bytes)
{
if (bytes != NULL) {
return (boolByte)(setTimeSignatureBeatsPerMeasure(bytes[0]) &&
setTimeSignatureNoteValue((unsigned const short)powl(2, bytes[1])));
}
return false;
}
void freeAudioSettings(void)
{
free(audioSettingsInstance);
audioSettingsInstance = NULL;
}
|