/
PT8211_stereo_16bits.ino
110 lines (67 loc) · 2.49 KB
/
PT8211_stereo_16bits.ino
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
/* Example of simple and stereo synthesis,
using Mozzi sonification library and an external dual DAC PT8211 (inspired by: https://sparklogic.ru/code-snipplets/i2s-example-code.html)
using an user-defined audioOutput() function. I2S, the protocol used by this DAC, is here emulated in synced way using SPI.
#define EXTERNAL_AUDIO_OUTPUT true should be uncommented in mozzi_config.h.
#define AUDIO_CHANNELS STEREO should be set in mozzi_config.h
Circuit:
PT8211 // Connect to:
------- -----------
Vdd V+
BCK SCK
WS any digital pin (defined by WS_pin here, pin 5)
DIN MOSI
GND GND
L/R to audio outputs
Mozzi documentation/API
https://sensorium.github.io/Mozzi/doc/html/index.html
Mozzi help/discussion/announcements:
https://groups.google.com/forum/#!forum/mozzi-users
Tim Barrass 2012, CC by-nc-sa.
T. Combriat 2020, CC by-nc-sa.
*/
#include <MozziGuts.h>
#include <Oscil.h>
#include <tables/cos2048_int8.h> // table for Oscils to play
#include <SPI.h>
#define CONTROL_RATE 256 // Hz, powers of 2 are most reliable
// Synthesis part
Oscil<COS2048_NUM_CELLS, AUDIO_RATE> aCos1(COS2048_DATA);
Oscil<COS2048_NUM_CELLS, AUDIO_RATE> aCos2(COS2048_DATA);
Oscil<COS2048_NUM_CELLS, CONTROL_RATE> kEnv1(COS2048_DATA);
Oscil<COS2048_NUM_CELLS, CONTROL_RATE> kEnv2(COS2048_DATA);
// External audio output parameters
#define WS_pin 5 // channel select pin for the DAC
//#define AUDIO_BIAS 0 // this DAC works on 0-centered signals
void audioOutput(const AudioOutput f) // f is a structure containing both channels
{
/* Note:
* the digital writes here can be optimised using portWrite if more speed is needed
*/
digitalWrite(WS_pin, LOW); //select Right channel
SPI.transfer16(f.r());
digitalWrite(WS_pin, HIGH); // select Left channel
SPI.transfer16(f.l());
}
void setup() {
pinMode(WS_pin, OUTPUT);
// Initialising the SPI connection on default port
SPI.begin();
SPI.beginTransaction(SPISettings(20000000, MSBFIRST, SPI_MODE0)); //MSB first, according to the DAC spec
aCos1.setFreq(440.f);
aCos2.setFreq(220.f);
kEnv1.setFreq(0.25f);
kEnv2.setFreq(0.30f);
startMozzi(CONTROL_RATE);
}
// Carry enveloppes
int env1, env2;
void updateControl() {
env1 = kEnv1.next();
env2 = kEnv2.next();
}
AudioOutput_t updateAudio() {
return StereoOutput::from16Bit(aCos1.next() * env1, aCos2.next() * env2);
}
void loop() {
audioHook();
}