Skip to content

Commit d3ada6f

Browse files
committed
MBED Support
1 parent 5ec23ec commit d3ada6f

File tree

13 files changed

+215
-6
lines changed

13 files changed

+215
-6
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ sentence=Some useful audio processing classes
66
paragraph=Timer, RingBuffer, I2S
77
category=Device Control
88
url=https://github.com/pschatzmann/arduino-audio-tools
9-
architectures=esp32
9+
architectures=esp32,mbed_nano

sandbox/streams-mozzi-udp/README.md renamed to sandbox/experiment-mozzi-udp/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Using Mozzi with UDP
22

3-
I am providing a simple integration for [Mozzi](https://sensorium.github.io/Mozzi/). Unfortunatly I needed to make some small changes to make things work together. Those can be found in my [fork](https://github.com/pschatzmann/Mozzi).
3+
I am providing a simple integration for [Mozzi](https://sensorium.github.io/Mozzi/).
44

55
We can send the output via TCP/IP or UDP. In this example I am using UDP:
66

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Using Mozzi with USB Audio
2+
3+
I am providing a simple integration for [Mozzi](https://sensorium.github.io/Mozzi/).
4+
5+
For ARM Mbed devices we can use the microprocessor as USB Audio Device
6+
7+
8+
## Result
9+
10+
No USB Audio available! Hmm, this is stragne...
11+
12+
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/**
2+
* @file streams-generator-usb.ino
3+
* @author Phil Schatzmann
4+
* @brief see https://github.com/pschatzmann/arduino-audio-tools/blob/main/examples/streams-generator-usb/README.md
5+
*
6+
* @author Phil Schatzmann
7+
* @copyright GPLv3
8+
*
9+
*/
10+
#include "AudioUSB.h"
11+
#include "AudioTools.h"
12+
#include "AudioMozzi.h"
13+
#include <Oscil.h>
14+
#include <tables/cos2048_int8.h> // table for Oscils to play
15+
#include <mozzi_fixmath.h>
16+
#include <EventDelay.h>
17+
#include <mozzi_rand.h>
18+
#include <mozzi_midi.h>
19+
20+
using namespace audio_tools;
21+
22+
typedef int16_t sound_t; // sound will be represented as int16_t (with 2 bytes)
23+
uint8_t channels = 2; // The stream will have 2 channels
24+
MozziGenerator mozzi(CONTROL_RATE); // subclass of SoundGenerator
25+
GeneratedSoundStream<sound_t> in(mozzi, channels); // Stream generated with mozzi
26+
AudioUSB out; // UDP Output - A2DPStream is a singleton!
27+
StreamCopy copier(out, in); // copy in to out
28+
29+
/// Copied from AMsynth.ino
30+
#define CONTROL_RATE 64 // Hz, powers of 2 are most reliable
31+
32+
// audio oscils
33+
Oscil<COS2048_NUM_CELLS, AUDIO_RATE> aCarrier(COS2048_DATA);
34+
Oscil<COS2048_NUM_CELLS, AUDIO_RATE> aModulator(COS2048_DATA);
35+
Oscil<COS2048_NUM_CELLS, AUDIO_RATE> aModDepth(COS2048_DATA);
36+
37+
// for scheduling note changes in updateControl()
38+
EventDelay kNoteChangeDelay;
39+
40+
// synthesis parameters in fixed point formats
41+
Q8n8 ratio; // unsigned int with 8 integer bits and 8 fractional bits
42+
Q24n8 carrier_freq; // unsigned long with 24 integer bits and 8 fractional bits
43+
Q24n8 mod_freq; // unsigned long with 24 integer bits and 8 fractional bits
44+
45+
// for random notes
46+
Q8n0 octave_start_note = 42;
47+
48+
49+
void setup(){
50+
Serial.begin(115200);
51+
52+
// setup AudioUSB
53+
out.begin(mozzi.config().sample_rate,mozzi.config().channels );
54+
55+
// setup Mozzi
56+
ratio = float_to_Q8n8(3.0f); // define modulation ratio in float and convert to fixed-point
57+
kNoteChangeDelay.set(200); // note duration ms, within resolution of CONTROL_RATE
58+
aModDepth.setFreq(13.f); // vary mod depth to highlight am effects
59+
randSeed(); // reseed the random generator for different results each time the sketch runs
60+
in.begin();
61+
}
62+
63+
void updateControl(){
64+
static Q16n16 last_note = octave_start_note;
65+
66+
if(kNoteChangeDelay.ready()){
67+
68+
// change octave now and then
69+
if(rand((byte)5)==0){
70+
last_note = 36+(rand((byte)6)*12);
71+
}
72+
73+
// change step up or down a semitone occasionally
74+
if(rand((byte)13)==0){
75+
last_note += 1-rand((byte)3);
76+
}
77+
78+
// change modulation ratio now and then
79+
if(rand((byte)5)==0){
80+
ratio = ((Q8n8) 1+ rand((byte)5)) <<8;
81+
}
82+
83+
// sometimes add a fractionto the ratio
84+
if(rand((byte)5)==0){
85+
ratio += rand((byte)255);
86+
}
87+
88+
// step up or down 3 semitones (or 0)
89+
last_note += 3 * (1-rand((byte)3));
90+
91+
// convert midi to frequency
92+
Q16n16 midi_note = Q8n0_to_Q16n16(last_note);
93+
carrier_freq = Q16n16_to_Q24n8(Q16n16_mtof(midi_note));
94+
95+
// calculate modulation frequency to stay in ratio with carrier
96+
mod_freq = (carrier_freq * ratio)>>8; // (Q24n8 Q8n8) >> 8 = Q24n8
97+
98+
// set frequencies of the oscillators
99+
aCarrier.setFreq_Q24n8(carrier_freq);
100+
aModulator.setFreq_Q24n8(mod_freq);
101+
102+
// reset the note scheduler
103+
kNoteChangeDelay.start();
104+
}
105+
}
106+
107+
AudioOutput_t updateAudio(){
108+
int32_t mod = (128u+ aModulator.next()) * ((byte)128+ aModDepth.next());
109+
return MonoOutput::fromNBit(24, mod * aCarrier.next());
110+
}
111+
112+
// Arduino loop
113+
void loop() {
114+
if (out)
115+
copier.copy();
116+
}

src/AudioConfig.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
*/
4646
#ifdef ESP32
4747
#include "esp32-hal-log.h"
48+
#define I2S_SUPPORT
4849
#define PIN_I2S_BCK 14
4950
#define PIN_I2S_WS 15
5051
#define PIN_I2S_DATA_IN 32
@@ -56,6 +57,7 @@
5657
#endif
5758

5859
#ifdef ESP8266
60+
#define I2S_SUPPORT
5961
#define PIN_I2S_BCK -1
6062
#define PIN_I2S_WS -1
6163
#define PIN_I2S_DATA_IN -1

src/AudioMozzi.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
#include "mozzi_config.h"
44
#include "hardware_defines.h"
55
#include "mozzi_analog.h"
6-
#include "MozziGuts.h"
6+
#include "Mozzi.h"
77

8-
extern uint64_t samples_written_to_buffer;
98

109
namespace audio_tools {
1110

@@ -54,6 +53,7 @@ class MozziGenerator : public SoundGenerator<int16_t> {
5453
AudioBaseInfo info;
5554
int control_counter_max;
5655
int control_counter;
56+
uint64_t samples_written_to_buffer;
5757

5858
};
5959

src/AudioTools/AudioI2S.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "AudioConfig.h"
4+
#ifdef I2S_SUPPORT
45
#include "AudioTypes.h"
56
#include "I2S_ESP32.h"
67
#include "I2S_ESP8266.h"
@@ -48,3 +49,5 @@ class I2S : public I2SBase {
4849

4950

5051
}
52+
53+
#endif

src/AudioTools/AudioLogger.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "AudioConfig.h"
44
#include "Stream.h"
5+
#include <cstdarg>
56

67
#ifndef PRINTF_BUFFER_SIZE
78
#define PRINTF_BUFFER_SIZE 160

src/AudioTools/AudioTypes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ enum I2SMode {
121121
};
122122

123123

124+
#ifdef I2S_SUPPORT
125+
124126
/**
125127
* @brief configuration for all common i2s settings
126128
* @author Phil Schatzmann
@@ -156,6 +158,7 @@ class I2SConfig {
156158

157159
};
158160

161+
#endif
159162

160163
/**
161164
* @brief E.g. used by Encoders and Decoders

0 commit comments

Comments
 (0)