Skip to content

Commit da600dc

Browse files
committed
fix broken sound generator
1 parent 49cdf94 commit da600dc

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

examples-desktop/generator/generator.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using namespace audio_tools;
44

55
uint16_t sample_rate=44100;
6-
uint8_t channels = 2; // The stream will have 2 channels
6+
uint8_t channels = 1; // The stream will have 2 channels
77
SineWaveGenerator<int16_t> sineWave(32000); // subclass of SoundGenerator with max amplitude of 32000
88
GeneratedSoundStream<int16_t> in(sineWave, channels); // Stream generated from sine wave
99
DefaultStream out; // On desktop we use
@@ -22,7 +22,7 @@ void setup(void) {
2222
out.begin(config);
2323

2424
// Setup sine wave
25-
sineWave.begin(sample_rate, N_B4);
25+
sineWave.begin(channels, sample_rate, N_B4);
2626

2727
}
2828

src/AudioTools/SoundGenerator.h

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,14 @@ class SoundGenerator {
4343
/// Provides a single sample
4444
virtual T readSample() = 0;
4545

46-
/// Provides the data as byte array with the requested number of channels
47-
virtual size_t readBytes( uint8_t *buffer, size_t lengthBytes){
48-
return readBytes(buffer, lengthBytes);
49-
}
5046

5147
/// Provides the data as byte array with the requested number of channels
52-
virtual size_t readBytes( uint8_t *buffer, size_t lengthBytes, uint8_t channels){
48+
virtual size_t readBytes( uint8_t *buffer, size_t lengthBytes){
5349
LOGD("readBytes: %d - channesl = %d",lengthBytes, channels);
5450
size_t result = 0;
5551
int frame_size = sizeof(T) * channels;
5652
if (active){
57-
switch (channels){
53+
switch (channels()){
5854
case 1:
5955
result = readSamples((T*) buffer, lengthBytes / frame_size) ;
6056
break;
@@ -93,12 +89,23 @@ class SoundGenerator {
9389
return active;
9490
}
9591

92+
void setChannels(int channels){
93+
output_channels = channels;
94+
}
95+
96+
int channels() {
97+
return output_channels;
98+
}
99+
96100
protected:
97101
bool active = false;
98102
bool activeWarningIssued = false;
103+
int output_channels = 1;
99104

100105
};
101106

107+
108+
102109
/**
103110
* @brief Generates a Sound with the help of sin() function.
104111
* @author Phil Schatzmann
@@ -115,8 +122,15 @@ class SineWaveGenerator : public SoundGenerator<T> {
115122
m_phase = phase;
116123
}
117124

118-
/// Starts the processing by defining the sample rate and frequency
119-
void begin(uint16_t sample_rate=44100, uint16_t frequency=0){
125+
void begin() {
126+
b
127+
egin(1, 44100, 0);
128+
}
129+
void begin(uint16_t sample_rate, uint16_t frequency=0){
130+
begin(1, sample_rate, frequency);
131+
}
132+
133+
void begin(int channels, uint16_t sample_rate, uint16_t frequency=0){
120134
LOGI("SineWaveGenerator::begin");
121135
this->m_frequency = frequency;
122136
this->m_sample_rate = sample_rate;

0 commit comments

Comments
 (0)