Permalink
Browse files

* c/bloopsaphone.c: freeing of memory.

  • Loading branch information...
1 parent 76964f9 commit 314696689527884bf5ce1f12cac20fe3fa7e8e3d _why committed Feb 12, 2009
Showing with 61 additions and 15 deletions.
  1. +1 −1 COPYING
  2. +46 −2 c/bloopsaphone.c
  3. +5 −1 c/bloopsaphone.h
  4. +9 −10 c/bloopsawhat.c
  5. +0 −1 c/notation.rl
View
@@ -1,6 +1,6 @@
:$: BLOOPSAPHONE :$:
- Copyright (c) 2008 why the lucky stiff
+ Copyright (c) 2009 why the lucky stiff
Based on sfxr (c) 2007 Tomas Pettersson
(Also released under the MIT license)
View
@@ -94,6 +94,12 @@ bloops_clear(bloops *B)
}
void
+bloops_tempo(bloops *B, int tempo)
+{
+ B->tempo = tempo;
+}
+
+void
bloops_track_at(bloops *B, bloopsatrack *track, int num)
{
B->tracks[num] = track;
@@ -111,6 +117,12 @@ bloops_play(bloops *B)
B->play = BLOOPS_PLAY;
}
+int
+bloops_is_done(bloops *B)
+{
+ return B->play == BLOOPS_STOP;
+}
+
static void
bloops_synth(bloops *B, int length, float* buffer)
{
@@ -119,6 +131,7 @@ bloops_synth(bloops *B, int length, float* buffer)
while (length--)
{
int samplecount = 0;
+ int moreframes = 0;
float allsample = 0.0f;
for (t = 0; t < BLOOPS_MAX_TRACKS; t++)
@@ -146,8 +159,15 @@ bloops_synth(bloops *B, int length, float* buffer)
}
else if (A->frames < frames)
break;
- frames += (int)(tempo2frames(A->tempo) * (4.0f / note->duration));
+ frames += (int)(tempo2frames(B->tempo) * (4.0f / note->duration));
}
+
+ if (A->frames <= frames)
+ moreframes++;
+ }
+ else
+ {
+ moreframes++;
}
A->frames++;
@@ -291,6 +311,8 @@ bloops_synth(bloops *B, int length, float* buffer)
allsample += ssample;
}
+ if (moreframes == 0)
+ B->play = BLOOPS_STOP;
*buffer++ = allsample;
}
}
@@ -313,6 +335,19 @@ static int bloops_port_callback(const void *inputBuffer, void *outputBuffer,
}
bloopsaphone *
+bloops_square()
+{
+ bloopsaphone *P = (bloopsaphone *)calloc(sizeof(bloopsaphone), 1);
+ P->type = BLOOPS_SQUARE;
+ P->volume = 0.5f;
+ P->sustain = 0.3f;
+ P->decay = 0.4f;
+ P->freq = 0.3f;
+ P->lpf = 1.0f;
+ return P;
+}
+
+bloopsaphone *
bloops_load(char* filename)
{
bloopsaphone *P = NULL;
@@ -368,7 +403,8 @@ bloops *
bloops_new()
{
bloops *B = (bloops *)malloc(sizeof(bloops));
- B->volume = 0.20f;
+ B->volume = 0.10f;
+ B->tempo = 120;
B->play = BLOOPS_STOP;
bloops_clear(B);
@@ -394,3 +430,11 @@ bloops_destroy(bloops *B)
if (!--bloops_open)
Pa_Terminate();
}
+
+void
+bloops_track_destroy(bloopsatrack *track)
+{
+ if (track->notes != NULL)
+ free(track->notes);
+ free(track);
+}
View
@@ -41,7 +41,6 @@ typedef struct {
typedef struct {
bloopsaphone *P;
- int tempo;
int nlen, capa;
bloopsanote *notes;
@@ -66,6 +65,7 @@ typedef struct {
typedef struct {
void *stream;
+ int tempo;
float volume;
bloopsatrack *tracks[BLOOPS_MAX_TRACKS];
unsigned char play;
@@ -76,10 +76,14 @@ typedef struct {
//
bloops *bloops_new();
void bloops_destroy(bloops *);
+bloopsaphone *bloops_square();
bloopsaphone *bloops_load(char *);
void bloops_clear(bloops *);
+void bloops_tempo(bloops *, int tempo);
void bloops_track_at(bloops *, bloopsatrack *, int);
+void bloops_track_destroy(bloopsatrack *);
void bloops_play(bloops *);
+int bloops_is_done(bloops *);
bloopsatrack *bloops_track(bloops *, bloopsaphone *, char *, int);
bloopsatrack *bloops_track2(bloops *, bloopsaphone *, char *);
char *bloops_track_str(bloopsatrack *);
View
@@ -15,20 +15,19 @@ usage()
int
main(int argc, char *argv[])
{
+ char *str;
if (argc > 1) {
bloops *B = bloops_new();
- bloopsaphone *P = bloops_load("tone.bloo");
- bloopsaphone *P2 = bloops_load("shot.bloo");
- bloopsaphone *P3 = bloops_load("jump.bloo");
- bloopsatrack *track = bloops_track2(B, P, "C 4 A A B 4 A A");
- bloopsatrack *track2 = bloops_track2(B, P2, "4 C 4 A 4 B 4 A");
- bloopsatrack *track3 = bloops_track2(B, P3, "A C A 4 4 4 A A");
- printf("%s\n", bloops_track_str(track));
+ bloops_tempo(B, 320);
+ bloopsaphone *P = bloops_square();
+ bloopsatrack *track = bloops_track2(B, P, "c5 c6 b4 b5 d5 d6 e5 e6");
+ printf("%s\n", str = bloops_track_str(track));
bloops_track_at(B, track, 0);
- bloops_track_at(B, track2, 1);
- bloops_track_at(B, track3, 2);
bloops_play(B);
- sleep(8);
+ while (!bloops_is_done(B))
+ sleep(1);
+ free(P); free(str);
+ bloops_track_destroy(track);
bloops_destroy(B);
return 0;
}
View
@@ -115,7 +115,6 @@ bloops_track(bloops *B, bloopsaphone *P, char *track, int tracklen)
S->P = P;
S->nlen = 0;
S->capa = 1024;
- S->tempo = 120;
S->notes = (bloopsanote *)calloc(sizeof(bloopsanote), 1024);
p = track;

0 comments on commit 3146966

Please sign in to comment.