|
1 | | -/* Naive implementation of a subset of OpenAL used by OpenMRac, featuring low sound quality and no optimization. |
| 1 | +/* Copyright (c) 2022, Vojtěch Salajka. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. */ |
| 2 | + |
| 3 | +/* |
| 4 | + * Naive implementation of a subset of OpenAL used by OpenMRac, featuring low sound quality and no optimization. |
2 | 5 | * This should serve as a starting point on platforms where usable OpenAL implementation is not available. |
3 | 6 | * To use this, add -DUSE_MINIAL to CFLAGS and remove -lopenal from LFLAGS. |
4 | 7 | */ |
|
14 | 17 |
|
15 | 18 | #define MA_FREQ 22050 |
16 | 19 | #define MA_SAMPLES 1024 |
| 20 | +#define MA_LINEAR 1 // sample filtering: 0 - none, 1 - linear |
17 | 21 |
|
18 | 22 | struct ALCdevice |
19 | 23 | { |
@@ -88,7 +92,26 @@ static void ma_callback(void *userdata, Uint8 *stream, int len) |
88 | 92 | } |
89 | 93 | } |
90 | 94 | if (!src.playing) break; |
| 95 | +#if MA_LINEAR |
| 96 | + uint32_t ipos0 = src.pos; |
| 97 | + uint32_t ipos1 = ipos0 + 1; |
| 98 | + Sint16 smp0 = buff.samples[ipos0]; |
| 99 | + Sint16 smp1 = 0; |
| 100 | + if (ipos1 >= buff.samples.size()) |
| 101 | + { |
| 102 | + if (src.looping) |
| 103 | + { |
| 104 | + smp1 = buff.samples[0]; |
| 105 | + } |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + smp1 = buff.samples[ipos1]; |
| 110 | + } |
| 111 | + (*floatBuff)[i] += (float(smp0) + (float(smp1) - float(smp0)) * (src.pos - ipos0)) * src.gain; |
| 112 | +#else |
91 | 113 | (*floatBuff)[i] += buff.samples[src.pos] * src.gain; |
| 114 | +#endif |
92 | 115 | src.pos += src.pitch; |
93 | 116 | } |
94 | 117 | } |
@@ -229,7 +252,7 @@ void alListenerfv(ALenum param, const ALfloat *values) |
229 | 252 |
|
230 | 253 | void alBufferData(ALuint buffer, ALenum format, const ALvoid *data, ALsizei size, ALsizei freq) |
231 | 254 | { |
232 | | - if (buffer == 0 || format != AL_FORMAT_MONO16 || freq != MA_FREQ) return; |
| 255 | + if (buffer == 0 || format != AL_FORMAT_MONO16 || freq != MA_FREQ) return; // only 22050 Hz, 16-bit mono audio is currently supported |
233 | 256 | SDL_LockAudio(); |
234 | 257 | MA_Buffer& buff = (*bufferMap)[buffer]; |
235 | 258 | buff.samples.resize(size >> 1); |
|
0 commit comments