Skip to content

Commit c581934

Browse files
committed
bgm_xyscope: use an enum for sample size handling
1 parent 60641c1 commit c581934

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

src/modules/bgm_xyscope.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@
2424

2525
#define PLOSSFAC ((BUFFER_FRAMES) / 64)
2626

27+
enum samplesize {
28+
SIZE_8, SIZE_16
29+
};
30+
2731
static snd_pcm_t * scope_pcm;
2832
// Details on the sample format before conversion.
2933
// Two-channel (enables XY mode), if not, acts like a primitive X oscilloscope
3034
static int sf_2c;
31-
static int sf_16b;
35+
static enum samplesize sf_sampsize;
3236
static int sf_us;
3337
static int sf_usey;
3438
static int sf_forceon;
@@ -104,13 +108,13 @@ static void * thread_func(void * ign) {
104108
frames = snd_pcm_recover(scope_pcm, frames, 0);
105109
if (frames < 0)
106110
printf("Warning: reading totally failed: %i, %s\n", frames, snd_strerror(frames));
107-
if (sf_16b) {
111+
if (sf_sampsize == SIZE_16) {
108112
if (sf_us) {
109113
LD_ALGORITHM(unsigned short, 8, 0);
110114
} else {
111115
LD_ALGORITHM(unsigned short, 8, 0x80);
112116
}
113-
} else {
117+
} else if (sf_sampsize == SIZE_8) {
114118
if (sf_us) {
115119
LD_ALGORITHM(byte, 0, 0);
116120
} else {
@@ -231,23 +235,23 @@ int init(int modulen, char* argstr) {
231235
}
232236
free(ourarg);
233237
sf_2c = 0;
234-
sf_16b = 0;
238+
sf_sampsize = SIZE_8;
235239
sf_us = 0;
236240
if (sf_usey) {
237241
if (!(code = snd_pcm_set_params(scope_pcm, SND_PCM_FORMAT_S8, SND_PCM_ACCESS_RW_INTERLEAVED, 2, SAMPLE_RATE, 1, 1000))) {
238242
printf("Got BS8C2\n");
239243
sf_2c = 1;
240244
} else if (!(code = snd_pcm_set_params(scope_pcm, SND_PCM_FORMAT_S16, SND_PCM_ACCESS_RW_INTERLEAVED, 2, SAMPLE_RATE, 1, 1000))) {
241245
printf("Got BS16C2\n");
242-
sf_16b = 1;
246+
sf_sampsize = SIZE_16;
243247
sf_2c = 1;
244248
} else if (!(code = snd_pcm_set_params(scope_pcm, SND_PCM_FORMAT_U8, SND_PCM_ACCESS_RW_INTERLEAVED, 2, SAMPLE_RATE, 1, 1000))) {
245249
printf("Got BU8C2\n");
246250
sf_2c = 1;
247251
sf_us = 1;
248252
} else if (!(code = snd_pcm_set_params(scope_pcm, SND_PCM_FORMAT_U16, SND_PCM_ACCESS_RW_INTERLEAVED, 2, SAMPLE_RATE, 1, 1000))) {
249253
printf("Got BU16C2\n");
250-
sf_16b = 1;
254+
sf_sampsize = SIZE_16;
251255
sf_2c = 1;
252256
sf_us = 1;
253257
}
@@ -256,13 +260,13 @@ int init(int modulen, char* argstr) {
256260
printf("Got BS8C1\n");
257261
} else if (!(code = snd_pcm_set_params(scope_pcm, SND_PCM_FORMAT_S16, SND_PCM_ACCESS_RW_INTERLEAVED, 1, SAMPLE_RATE, 1, 1000))) {
258262
printf("Got BS16C1\n");
259-
sf_16b = 1;
263+
sf_sampsize = SIZE_16;
260264
} else if (!(code = snd_pcm_set_params(scope_pcm, SND_PCM_FORMAT_U8, SND_PCM_ACCESS_RW_INTERLEAVED, 1, SAMPLE_RATE, 1, 1000))) {
261265
printf("Got BU8C1\n");
262266
sf_us = 1;
263267
} else if (!(code = snd_pcm_set_params(scope_pcm, SND_PCM_FORMAT_U16, SND_PCM_ACCESS_RW_INTERLEAVED, 1, SAMPLE_RATE, 1, 1000))) {
264268
printf("Got BU16C1\n");
265-
sf_16b = 1;
269+
sf_sampsize = SIZE_16;
266270
sf_us = 1;
267271
} else {
268272
printf("Couldn't convince ALSA to give sane settings: %i\n", code);
@@ -271,7 +275,18 @@ int init(int modulen, char* argstr) {
271275
return 1;
272276
}
273277
}
274-
bufferA = malloc(BUFFER_FRAMES * (sf_2c ? 2 : 1) * (sf_16b ? 2 : 1));
278+
279+
int bytesPerSample = 1;
280+
switch (sf_sampsize) {
281+
case SIZE_8:
282+
bytesPerSample = 1;
283+
break;
284+
case SIZE_16:
285+
bytesPerSample = 2;
286+
break;
287+
}
288+
289+
bufferA = malloc(BUFFER_FRAMES * (sf_2c ? 2 : 1) * bytesPerSample);
275290
if (!bufferA) {
276291
printf("Couldn't allocate working buffer\n");
277292
snd_pcm_close(scope_pcm);

0 commit comments

Comments
 (0)