Skip to content

Commit 0dba2b6

Browse files
committed
Linear filtering for MiniAL
1 parent 16b31da commit 0dba2b6

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

src/minial.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
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.
25
* This should serve as a starting point on platforms where usable OpenAL implementation is not available.
36
* To use this, add -DUSE_MINIAL to CFLAGS and remove -lopenal from LFLAGS.
47
*/
@@ -14,6 +17,7 @@
1417

1518
#define MA_FREQ 22050
1619
#define MA_SAMPLES 1024
20+
#define MA_LINEAR 1 // sample filtering: 0 - none, 1 - linear
1721

1822
struct ALCdevice
1923
{
@@ -88,7 +92,26 @@ static void ma_callback(void *userdata, Uint8 *stream, int len)
8892
}
8993
}
9094
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
91113
(*floatBuff)[i] += buff.samples[src.pos] * src.gain;
114+
#endif
92115
src.pos += src.pitch;
93116
}
94117
}
@@ -229,7 +252,7 @@ void alListenerfv(ALenum param, const ALfloat *values)
229252

230253
void alBufferData(ALuint buffer, ALenum format, const ALvoid *data, ALsizei size, ALsizei freq)
231254
{
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
233256
SDL_LockAudio();
234257
MA_Buffer& buff = (*bufferMap)[buffer];
235258
buff.samples.resize(size >> 1);

src/minial.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
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+
13
#ifndef MINIAL_H
24
#define MINIAL_H
35

0 commit comments

Comments
 (0)