The Tooba
 All Data Structures Namespaces Files Functions Variables Macros Pages
instr.cpp
Go to the documentation of this file.
1 /*
2 We have a 40 kHz or 50 kHz timer interrupt that handles sound
3 samples. First it writes the previously computed sample to the serial DAC.
4 Then it computes the sample for the next time.
5 */
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <stdint.h>
10 
11 #define __ARDUINO 0
12 
13 #define HIGH 1
14 #define LOW 0
15 
16 #define ASSERT(cond) assertion(cond, #cond)
17 
18 #include "teensy/teensy.ino"
19 
20 #define _STEP 1.0594630943592953
21 #define _STEP2 (_STEP * _STEP)
22 #define _STEP4 (_STEP2 * _STEP2)
23 
24 #define _A (440.0 * 2 * VCO_MAX_VALUE / (4 * SAMPLING_RATE))
25 
26 #define F(oct) ((int) ((1 << oct) * _A / _STEP4))
27 #define Fsharp(oct) ((int) ((1 << oct) * _A / _STEP2 / _STEP))
28 #define G(oct) ((int) ((1 << oct) * _A / _STEP2))
29 #define Gsharp(oct) ((int) ((1 << oct) * _A / _STEP))
30 #define A(oct) ((int) ((1 << oct) * _A))
31 #define Bflat(oct) ((int) ((1 << oct) * _A * _STEP))
32 #define B(oct) ((int) ((1 << oct) * _A * _STEP2))
33 #define C(oct) ((int) ((1 << oct) * _A * _STEP2 * _STEP))
34 
35 void assertion(int cond, const char *strcond)
36 {
37  if (!cond) {
38  fprintf(stderr, "ASSERTION FAILED: %s\n", strcond);
39  exit(1);
40  }
41 }
42 
43 int main(void)
44 {
45  FILE *outf, *gp_outf;
46  int i, t;
47  int32_t y;
48 
49  setup();
50 
51  gp_outf = fopen("foo.gp", "w");
52 
53  outf = fopen("foo.py", "w");
54  fprintf(outf, "sampfreq = %lf\n", (double) SAMPLING_RATE);
55  fprintf(outf, "samples = [\n");
56 
57  v[0].setfreq(400);
58  v[1].setfreq(500);
59  v[2].setfreq(600);
60 
61  for (t = 0; t < 4 * SAMPLING_RATE; t++) {
63  ASSERT(samples.read((uint32_t *) &y) == 0);
64 
65  /* Numbers for Gnuplot */
66  fprintf(gp_outf, "%d %d %d\n", t, v[0].adsr.output() >> 23, (int) (y - 2048));
67 
68  /* Numbers for AIFF file */
69  fprintf(outf, "%ld,\n", (long int) (y << 5));
70 
71  if (t == SAMPLING_RATE / 2) {
72  for (i = 0; i < NUM_VOICES; i++)
73  v[i].keydown(1);
74  }
75  if (t == SAMPLING_RATE) {
76  for (i = 0; i < NUM_VOICES; i++)
77  v[i].keydown(0);
78  }
79  }
80 
81  fprintf(outf, "]\n");
82  fclose(outf);
83  fclose(gp_outf);
84 }
tuple adsr
Definition: testit.py:74
void setfreq(float f)
Definition: voice.h:152
void compute_sample(void)
Definition: teensy.ino:133
uint8_t read(uint32_t *x)
Definition: queue.h:50
void assertion(int cond, const char *strcond)
Definition: instr.cpp:35
#define ASSERT(cond)
Definition: instr.cpp:16
void setup()
Definition: teensy.ino:27
int main(void)
Definition: instr.cpp:43
Voice v[NUM_VOICES]
Definition: stub.cpp:6
#define NUM_VOICES
Definition: common.h:32
#define SAMPLING_RATE
Definition: common.h:27
Queue samples
Definition: teensy.ino:23