aboutsummaryrefslogtreecommitdiff
path: root/common.c
diff options
context:
space:
mode:
authorDavide Morelli <morellid@users.sourceforge.net>2005-11-30 17:31:33 +0000
committerDavide Morelli <morellid@users.sourceforge.net>2005-11-30 17:31:33 +0000
commit84cff02e8cf3b773d2370d8020b8d1f4b95bcda9 (patch)
tree36bbf97a207a6a28481f724825c3e248dcffabd0 /common.c
parent8911ddbc575c4714c5ac294dd32bbd9ab4278a2d (diff)
going little further with common functions
svn path=/trunk/externals/frankenstein/; revision=4091
Diffstat (limited to 'common.c')
-rwxr-xr-xcommon.c104
1 files changed, 100 insertions, 4 deletions
diff --git a/common.c b/common.c
index 1eb896f..6f82204 100755
--- a/common.c
+++ b/common.c
@@ -1,21 +1,27 @@
#include "common.h"
+#include <math.h>
+#include <stdlib.h>
+
+#include "m_pd.h" // to post errors and debug messages
+
t_duration int2duration(int n)
{
t_duration dur;
- int curr, i, j, ok;
+ int curr, i, j, ok, currden;
curr=0;
ok=0;
for (i=0; i<num_possible_denominators; i++)
{
- for (j=0; j<i; j++)
+ currden = possible_denominators[i];
+ for (j=0; j<currden; j++)
{
if (curr==n)
{
dur.numerator=j;
- dur.denominator=i;
- j=i;
+ dur.denominator=currden;
+ j=currden;
i=num_possible_denominators;
ok=1;
} else
@@ -60,4 +66,94 @@ int possible_durations()
}
ris += 1;
return ris;
+}
+
+t_duration float2duration(float fduration)
+{
+ float minDiff, currfduration, currDiff;
+ int i, maxi;
+ t_duration currDur, bestDur;
+
+ bestDur.numerator=1;
+ bestDur.denominator=1;
+ minDiff = 1;
+ maxi = possible_durations();
+ for (i=0; i<maxi; i++)
+ {
+ // post("i=%i", i);
+ currDur = int2duration(i);
+ // post("currDur=%i/%i", currDur.numerator, currDur.denominator);
+ currfduration = duration2float(currDur);
+ // post("currfduration=%f", currfduration);
+ currDiff = (float) fabs(fduration - currfduration);
+ if (currDiff<=minDiff)
+ {
+ minDiff = currDiff;
+ bestDur = currDur;
+ }
+ }
+ return bestDur;
+}
+
+float duration2float(t_duration duration)
+{
+ return (float) (((float)duration.numerator) / ((float)duration.denominator));
+}
+
+void setFirstBeat(t_rhythm_event **firstEvent, unsigned short int voice, float fduration)
+{
+ t_duration res;
+ t_rhythm_event *newElement;
+ // convert from float to duration
+ res = float2duration(fduration);
+ // allocate a new element of the list
+ newElement = malloc(sizeof(t_rhythm_event));
+ // set the pointers
+ newElement->previous = 0;
+ newElement->next = 0;
+ newElement->voice=voice;
+ newElement->duration.numerator = res.numerator;
+ newElement->duration.denominator = res.denominator;
+ *firstEvent = newElement;
+}
+
+void concatenateBeat(t_rhythm_event *currentEvent, unsigned short int voice, float fduration)
+{
+ t_duration res;
+ t_rhythm_event *newElement, *lastElement;
+ lastElement = currentEvent;
+ while(lastElement->next)
+ lastElement = lastElement->next;
+ // convert from float to duration
+ res = float2duration(fduration);
+ // allocate a new element of the list
+ newElement = (t_rhythm_event *) malloc(sizeof(t_rhythm_event));
+ // set the pointers
+ newElement->previous = lastElement;
+ newElement->next = 0;
+ lastElement->next = newElement;
+ newElement->voice=voice;
+ newElement->duration.numerator = res.numerator;
+ newElement->duration.denominator = res.denominator;
+
+}
+
+void freeBeats(t_rhythm_event *currentEvent)
+{
+ t_rhythm_event *prev;
+ t_rhythm_event *next;
+
+ // go to the first element of the list
+ while(currentEvent->previous)
+ currentEvent = currentEvent->previous;
+
+ // now free each element
+ next=currentEvent->next;
+ do
+ {
+ prev = currentEvent;
+ next = currentEvent->next;
+ free(currentEvent);
+ } while(next);
+
} \ No newline at end of file