Skip to content

Commit c9af93f

Browse files
committed
code for speaker
1 parent 83e162e commit c9af93f

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

src/mic.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ MicManager::MicManager(int bckPin,
3232
I2SStream i2sStream;
3333
cfg = i2sStream.defaultConfig(RX_MODE);
3434

35-
cfg.i2s_format = I2S_STD_FORMAT; // optional because default setting
35+
cfg.i2s_format = I2S_STD_FORMAT;
3636
cfg.bits_per_sample = BITS_PER_SAMPLE;
3737
cfg.channels = CHANNELS;
3838
cfg.sample_rate = SAMPLE_RATE;
@@ -56,7 +56,9 @@ MicManager::startRecording(String fileName)
5656

5757
file = SD.open(fileName, FILE_WRITE);
5858
file.seek(0);
59+
5960
copier.setCheckAvailableForWrite(false);
61+
6062
i2sStream.begin(cfg);
6163
copier.begin(file, i2sStream);
6264
}

src/mic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class MicManager
1919
int sdMOSIPin,
2020
int sdCLKPin);
2121
void startRecording(String fileName);
22-
void stopRecording();
2322
void record();
23+
void stopRecording();
2424

2525
private:
2626
int mic_BCK, mic_WS, mic_DATA;

src/speaker.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,46 @@
11
#include "speaker.h"
2+
3+
#define SAMPLE_RATE 16000
4+
#define CHANNELS 1
5+
#define BITS_PER_SAMPLE 32
6+
7+
// Source: SD card over i2c
8+
SPIClass sd_spi;
9+
File file;
10+
11+
// Destination: PCM out via one wire to AMP -> speakers
12+
#define PIN_AMP_LEFT_CHANNEL 10
13+
#define PIN_AMP_RIGHT_CHANNEL 11
14+
15+
Speaker::Speaker(int sdCSPin, int sdMISOPin, int sdMOSIPin, int sdCLKPin)
16+
: sd_CS(sdCSPin)
17+
, sd_MISO(sdMISOPin)
18+
, sd_MOSI(sdMOSIPin)
19+
, sd_CLK(sdCLKPin)
20+
{
21+
sd_spi.begin(sdCLKPin, sdMISOPin, sdMOSIPin, sdCSPin);
22+
}
23+
24+
void
25+
Speaker::play(String fileName)
26+
{
27+
if (!SD.begin(sd_CS, sd_spi)) {
28+
printf("SD card failed to initialize");
29+
return;
30+
}
31+
32+
file = SD.open(fileName, FILE_READ);
33+
34+
size_t bytesRead = 1;
35+
uint8_t buffer[SAMPLE_RATE];
36+
37+
while (bytesRead > 0) {
38+
bytesRead = file.read(buffer, sizeof(buffer));
39+
40+
for (size_t i = 0; i < bytesRead; i++) {
41+
dacWrite(PIN_AMP_LEFT_CHANNEL, buffer[i]);
42+
dacWrite(PIN_AMP_RIGHT_CHANNEL, buffer[i]);
43+
delayMicroseconds(1000000 / SAMPLE_RATE);
44+
}
45+
}
46+
}

src/speaker.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// speaker.h
2+
#ifndef SPEAKER_H
3+
#define SPEAKER_H
4+
5+
#include "AudioLibs/AudioSourceSD.h"
6+
#include "AudioTools.h"
7+
8+
class Speaker
9+
{
10+
public:
11+
Speaker(int sdCSPin, int sdMISOPin, int sdMOSIPin, int sdCLKPin);
12+
13+
void play(String fileName);
14+
15+
private:
16+
int sd_CS, sd_MISO, sd_MOSI, sd_CLK;
17+
};
18+
19+
#endif // SPEAKER_H

0 commit comments

Comments
 (0)