shithub: cstory

Download patch

ref: eed610e8bed757b193e8d9b17562db3f7775c937
parent: aa6147bf1c3ab5d88a80abffc0c515f5abfbd814
author: Jacob Moody <moody@posixcafe.org>
date: Fri Dec 15 00:10:35 EST 2023

bad deep fried audio

--- a/mkfile
+++ b/mkfile
@@ -79,9 +79,11 @@
 	src/Triangle.$O\
 	src/ValueView.$O\
 	src/Backends/Platform/9front.$O\
-	src/Backends/Audio/9front.$O\
 	src/Backends/Controller/9front.$O\
 	src/Backends/Rendering/9front.$O\
+	src/Backends/Audio/SoftwareMixer.$O\
+	src/Backends/Audio/SoftwareMixer/Mixer.$O\
+	src/Backends/Audio/SoftwareMixer/9front.$O\
 
 ASSETS=\
 	src/Resource/BITMAP/Credit01.bmp.h\
--- a/src/Backends/Audio/9front.cpp
+++ /dev/null
@@ -1,74 +1,0 @@
-// Released under the MIT licence.
-// See LICENCE.txt for details.
-
-#include "../Audio.h"
-
-#include <stddef.h>
-
-int AudioBackend_Init(void)
-{
-	return 1;
-}
-
-void AudioBackend_Deinit(void)
-{
-	
-}
-
-AudioBackend_Sound* AudioBackend_CreateSound(unsigned int frequency, const unsigned char *samples, size_t length)
-{
-	(void)frequency;
-	(void)samples;
-	(void)length;
-
-	return NULL;
-}
-
-void AudioBackend_DestroySound(AudioBackend_Sound *sound)
-{
-	(void)sound;
-}
-
-void AudioBackend_PlaySound(AudioBackend_Sound *sound, int looping)
-{
-	(void)sound;
-	(void)looping;
-}
-
-void AudioBackend_StopSound(AudioBackend_Sound *sound)
-{
-	(void)sound;
-}
-
-void AudioBackend_RewindSound(AudioBackend_Sound *sound)
-{
-	(void)sound;
-}
-
-void AudioBackend_SetSoundFrequency(AudioBackend_Sound *sound, unsigned int frequency)
-{
-	(void)sound;
-	(void)frequency;
-}
-
-void AudioBackend_SetSoundVolume(AudioBackend_Sound *sound, long volume)
-{
-	(void)sound;
-	(void)volume;
-}
-
-void AudioBackend_SetSoundPan(AudioBackend_Sound *sound, long pan)
-{
-	(void)sound;
-	(void)pan;
-}
-
-void AudioBackend_SetOrganyaCallback(void (*callback)(void))
-{
-	(void)callback;
-}
-
-void AudioBackend_SetOrganyaTimer(unsigned int milliseconds)
-{
-	(void)milliseconds;
-}
--- a/src/Backends/Audio/SoftwareMixer.cpp
+++ b/src/Backends/Audio/SoftwareMixer.cpp
@@ -58,7 +58,7 @@
 	SoftwareMixerBackend_UnlockOrganyaMutex();
 }
 
-bool AudioBackend_Init(void)
+int AudioBackend_Init(void)
 {
 	output_frequency = SoftwareMixerBackend_Init(MixSoundsAndUpdateOrganya);
 
@@ -67,17 +67,17 @@
 		Mixer_Init(output_frequency);
 
 		if (SoftwareMixerBackend_Start())
-			return true;
+			return 1;
 
 		SoftwareMixerBackend_Deinit();
 	}
 
-	return false;
+	return 0;
 }
 
 void AudioBackend_Deinit(void)
 {
-	return SoftwareMixerBackend_Deinit();
+	SoftwareMixerBackend_Deinit();
 }
 
 AudioBackend_Sound* AudioBackend_CreateSound(unsigned int frequency, const unsigned char *samples, size_t length)
@@ -103,7 +103,7 @@
 	SoftwareMixerBackend_UnlockMixerMutex();
 }
 
-void AudioBackend_PlaySound(AudioBackend_Sound *sound, bool looping)
+void AudioBackend_PlaySound(AudioBackend_Sound *sound, int looping)
 {
 	if (sound == NULL)
 		return;
--- /dev/null
+++ b/src/Backends/Audio/SoftwareMixer/9front.cpp
@@ -1,0 +1,97 @@
+// Released under the MIT licence.
+// See LICENCE.txt for details.
+
+#include "Backend.h"
+
+#include <stddef.h>
+#include <string.h>
+
+#include "../../Misc.h"
+
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+
+static void (*parent_callback)(long *stream, size_t frames_total);
+
+static void Callback(u8int *stream_uint8, int len)
+{
+	long mix_buffer[0x800 * 2];
+	short *stream = (short*)stream_uint8;
+	const size_t frames_total = len / sizeof(short) / 2;
+
+	size_t frames_done = 0;
+
+	while (frames_done != frames_total)
+	{
+
+		size_t subframes = MIN(0x800, frames_total - frames_done);
+
+		memset(mix_buffer, 0, subframes * sizeof(long) * 2);
+
+		parent_callback(mix_buffer, subframes);
+
+		for (size_t i = 0; i < subframes * 2; ++i)
+		{
+			if (mix_buffer[i] > 0x7FFF)
+				*stream++ = 0x7FFF;
+			else if (mix_buffer[i] < -0x7FFF)
+				*stream++ = -0x7FFF;
+			else
+				*stream++ = mix_buffer[i];
+		}
+
+		frames_done += subframes;
+	}
+}
+
+static uchar audiobuf[8192];
+static Lock mixer, audio;
+
+static void SoftwareMixerBackend_Proc(void*)
+{
+	int fd;
+
+	fd = open("/dev/audio", OWRITE);
+	if(fd < 0)
+		return;
+	for(;;){
+		Callback(audiobuf, sizeof audiobuf);
+		write(fd, audiobuf, sizeof audiobuf);
+	}
+}
+
+unsigned long SoftwareMixerBackend_Init(void (*callback)(long *stream, size_t frames_total))
+{
+	parent_callback = callback;
+	proccreate(SoftwareMixerBackend_Proc, nil, 8192);
+	return 44100;
+}
+
+void SoftwareMixerBackend_Deinit(void)
+{
+
+}
+
+int SoftwareMixerBackend_Start(void)
+{
+	return 1;
+}
+
+void SoftwareMixerBackend_LockMixerMutex(void)
+{
+	lock(&mixer);
+}
+
+void SoftwareMixerBackend_UnlockMixerMutex(void)
+{
+	unlock(&mixer);
+}
+
+void SoftwareMixerBackend_LockOrganyaMutex(void)
+{
+	lock(&audio);
+}
+
+void SoftwareMixerBackend_UnlockOrganyaMutex(void)
+{
+	unlock(&audio);
+}
--- a/src/Backends/Audio/SoftwareMixer/Backend.h
+++ b/src/Backends/Audio/SoftwareMixer/Backend.h
@@ -8,7 +8,7 @@
 unsigned long SoftwareMixerBackend_Init(void (*callback)(long *stream, size_t frames_total));
 void SoftwareMixerBackend_Deinit(void);
 
-bool SoftwareMixerBackend_Start(void);
+int SoftwareMixerBackend_Start(void);
 
 void SoftwareMixerBackend_LockMixerMutex(void);
 void SoftwareMixerBackend_UnlockMixerMutex(void);
--- a/src/Backends/Audio/SoftwareMixer/Mixer.cpp
+++ b/src/Backends/Audio/SoftwareMixer/Mixer.cpp
@@ -15,6 +15,7 @@
 
 #define LANCZOS_KERNEL_RADIUS 2
 
+typedef struct Mixer_Sound Mixer_Sound;
 struct Mixer_Sound
 {
 	signed char *samples;
@@ -22,8 +23,8 @@
 	size_t position;
 	unsigned short position_subsample;
 	unsigned long advance_delta; // 16.16 fixed-point
-	bool playing;
-	bool looping;
+	int playing;
+	int looping;
 	short volume;    // 8.8 fixed-point
 	short pan_l;     // 8.8 fixed-point
 	short pan_r;     // 8.8 fixed-point
@@ -77,7 +78,7 @@
 		sound->samples[i] = samples[i] - 0x80;	// Convert from unsigned 8-bit PCM to signed
 
 	sound->frames = length;
-	sound->playing = false;
+	sound->playing = 0;
 	sound->position = 0;
 	sound->position_subsample = 0;
 
@@ -108,9 +109,9 @@
 	}
 }
 
-void Mixer_PlaySound(Mixer_Sound *sound, bool looping)
+void Mixer_PlaySound(Mixer_Sound *sound, int looping)
 {
-	sound->playing = true;
+	sound->playing = 1;
 	sound->looping = looping;
 
 	// Fill the out-of-bounds part of the buffer with
@@ -139,7 +140,7 @@
 
 void Mixer_StopSound(Mixer_Sound *sound)
 {
-	sound->playing = false;
+	sound->playing = 0;
 }
 
 void Mixer_RewindSound(Mixer_Sound *sound)
@@ -233,7 +234,7 @@
 					}
 					else
 					{
-						sound->playing = false;
+						sound->playing = 0;
 						sound->position = 0;
 						sound->position_subsample = 0;
 						break;
--- a/src/Backends/Audio/SoftwareMixer/Mixer.h
+++ b/src/Backends/Audio/SoftwareMixer/Mixer.h
@@ -10,7 +10,7 @@
 void Mixer_Init(unsigned long frequency);
 Mixer_Sound* Mixer_CreateSound(unsigned int frequency, const unsigned char *samples, size_t length);
 void Mixer_DestroySound(Mixer_Sound *sound);
-void Mixer_PlaySound(Mixer_Sound *sound, bool looping);
+void Mixer_PlaySound(Mixer_Sound *sound, int looping);
 void Mixer_StopSound(Mixer_Sound *sound);
 void Mixer_RewindSound(Mixer_Sound *sound);
 void Mixer_SetSoundFrequency(Mixer_Sound *sound, unsigned int frequency);