Skip to content

Commit a34dada

Browse files
committed
remove setBitsPerSample since this must be 16
1 parent 22d3c7d commit a34dada

File tree

3 files changed

+19
-70
lines changed

3 files changed

+19
-70
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"files.associations": {
3-
"types": "cpp"
3+
"types": "cpp",
4+
"algorithm": "cpp"
45
}
56
}

src/sam_arduino.h

Lines changed: 16 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -200,41 +200,29 @@ class SAM {
200200
}
201201
}
202202

203-
/// Defines the bits per sample (default 8)
204-
void setOutputBitsPerSample(int bits_per_sample){
205-
SAM_LOG("setOutputBitsPerSample: %d", bits_per_sample);
206-
if ((bits_per_sample==8 || bits_per_sample==16) &&arduino_output->bitsPerSample()==-1){
207-
this->bits_per_sample = bits_per_sample;
208-
arduino_output->setBitsPerSample(bits_per_sample);
209-
} else {
210-
SAM_LOG("Bits Per Sample is not supported for this output type");
211-
}
212-
}
213-
214-
/// Enableds/disables the scaling to 16 bits - by default this is on
215-
void setTrue16Bits(bool active){
216-
true16Bits = active;
217-
}
218203

219204
/// Provides the sample rate (44100)
220205
static int sampleRate() {
221206
return SAMOutputBase::sampleRate();
222207
}
223208

224-
209+
/// Provides the bits per sample
225210
int bitsPerSample() {
226211
return bits_per_sample;
227212
}
228213

214+
/// Provides the number of channels
229215
int channels() {
230216
return channel_count;
231217
}
232218

233219
protected:
234220
SAMOutputBase *arduino_output=nullptr;
235-
int bits_per_sample = 8;
221+
int bits_per_sample = 16;
236222
int channel_count = 1;
237-
bool true16Bits = true;
223+
uint8_t audio[2];
224+
int16_t *p_audio = (int16_t*)&audio;
225+
uint8_t audio_byte = 0;
238226

239227
/// Used to feed the audio result back to this class
240228
static void outputByteCallback(void *cbdata, unsigned char b) {
@@ -248,51 +236,32 @@ class SAM {
248236
SAM_LOG("setOutput");
249237
arduino_output = out;
250238

251-
// synchronize output definition with arduino_output
239+
// synchronize channels definition with arduino_output: I2S must be 2 therefore we try to get it from arduino_output first
252240
if (arduino_output->channels()!=-1){
253241
this->channel_count = arduino_output->channels();
254242
} else {
255243
arduino_output->setChannels(channel_count);
256244
}
257-
if (arduino_output->bitsPerSample()!=-1){
258-
this->bits_per_sample = arduino_output->bitsPerSample();
259-
} else {
260-
arduino_output->setBitsPerSample(bits_per_sample);
261-
}
262245
SAM_LOG("-> channel_count: %d",this->channel_count);
263246
SAM_LOG("-> bits_per_sample: %d",this->bits_per_sample);
264247
}
265248

266-
/// Writes the data to the output. The data is provided as unsinged byte, so the values are between 0 and 256
249+
/// Writes the data to the output. The data is provided on 1 channel as unsinged byte of int16 values
267250
void write(uint8_t in) {
268251
SAM_LOG("SAM::write bps:%d / channels:%d",bits_per_sample, channel_count);
269-
// filter out noise
270-
uint8_t b = filter(in);
252+
audio[audio_byte++] = in;
271253

272-
// convert data
273-
if (bits_per_sample==8){
274-
uint8_t sample[channel_count];
275-
for (int j=0;j<channel_count;j++){
276-
sample[j] = b;
277-
}
278-
SAM_LOG("writing to %s", arduino_output->name());
279-
arduino_output->write((byte*)sample, channel_count);
280-
} else if (bits_per_sample==16) {
281-
int16_t s16 = b;
282-
if (true16Bits) {
283-
// convert to signed
284-
s16 -= 128;
285-
// convert to int16
286-
s16 *= 128;
287-
}
254+
if (audio_byte==2){
255+
audio_byte = 0;
256+
int16_t value = *p_audio;
257+
258+
// provide multiple channels if necessary
288259
int16_t sample[channel_count];
289-
for (int j=0;j<channel_count; j++){
290-
sample[j] = s16;
260+
for (int j=0;j<channel_count;j++){
261+
sample[j] = value;
291262
}
292263
SAM_LOG("writing to %s", arduino_output->name());
293264
arduino_output->write((byte*)sample, channel_count*2);
294-
} else {
295-
SAM_LOG("Error - invalid bits_per_sample");
296265
}
297266
}
298267

@@ -339,19 +308,6 @@ class SAM {
339308

340309
return true;
341310
}
342-
343-
// filter oscillating noise 80 112
344-
uint8_t filter(uint8_t b){
345-
static bool filter_active = false;
346-
bool last_filter_active = filter_active;
347-
348-
if (b==80 || b==112){
349-
filter_active = true;
350-
} else {
351-
filter_active = false;
352-
}
353-
return last_filter_active && filter_active ? 128 : b;
354-
}
355311
};
356312

357313
void printLog(char* msg){

src/sam_arduino_out.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class SAMOutputBase {
5757
}
5858

5959
virtual int bitsPerSample() {
60-
return bits_per_sample;
60+
return 16;
6161
}
6262

6363
virtual bool isOpen() {
@@ -72,10 +72,6 @@ class SAMOutputBase {
7272
SAM_sample_rate = rate; // 44100;
7373
}
7474

75-
void setBitsPerSample(int bps) {
76-
bits_per_sample = bps;
77-
}
78-
7975
virtual const char* name() = 0;
8076

8177
virtual bool write(byte *buff,int bytes_count) = 0;
@@ -163,10 +159,6 @@ class SAMOutputI2S : public SAMOutputBase {
163159
return 2;
164160
}
165161

166-
int bitsPerSample() {
167-
return 16;
168-
}
169-
170162
virtual void open() {
171163
// update sample sample_rate
172164
i2s_config.sample_rate = SAMOutputBase::sampleRate();

0 commit comments

Comments
 (0)