Original music (#393)

* adds support for GOG-format music ogg files

---------

Co-authored-by: Anonymous Maarten <madebr@users.noreply.github.com>
This commit is contained in:
Dethrace Engineering Department 2024-07-17 06:53:42 +12:00 committed by GitHub
parent a48c47beab
commit 6e6ce12eb7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
22 changed files with 92300 additions and 82414 deletions

View File

@ -64,6 +64,7 @@ endif()
add_subdirectory(lib/BRender-v1.3.2)
add_subdirectory(lib/libsmacker)
add_subdirectory(lib/miniaudio)
add_subdirectory(lib/stb)
add_library(compile_with_werror INTERFACE)

View File

@ -10,8 +10,9 @@ project(libsmacker C)
add_library(libsmacker STATIC)
target_include_directories(libsmacker PUBLIC
.
target_include_directories(libsmacker
SYSTEM PUBLIC
.
)
if(NOT MSVC)

View File

@ -1,20 +1,6 @@
add_library(miniaudio STATIC)
add_library(miniaudio INTERFACE)
target_include_directories(miniaudio
PUBLIC
target_include_directories(miniaudio
SYSTEM INTERFACE
include
PRIVATE
include/miniaudio
)
target_sources(miniaudio PRIVATE
miniaudio.c
)
if (CMAKE_C_COMPILER_ID MATCHES "Clang")
target_compile_options(miniaudio PRIVATE
-Wno-deprecated-declarations
)
endif()

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

6
lib/stb/CMakeLists.txt Normal file
View File

@ -0,0 +1,6 @@
add_library(stb INTERFACE)
target_include_directories(stb
SYSTEM INTERFACE
include
)

184
lib/stb/README.md Normal file
View File

@ -0,0 +1,184 @@
# https://github.com/nothings/stb/blob/master/README.md
stb
===
single-file public domain (or MIT licensed) libraries for C/C++
# This project discusses security-relevant bugs in public in Github Issues and Pull Requests, and it may take significant time for security fixes to be implemented or merged. If this poses an unreasonable risk to your project, do not use stb libraries.
Noteworthy:
* image loader: [stb_image.h](stb_image.h)
* image writer: [stb_image_write.h](stb_image_write.h)
* image resizer: [stb_image_resize2.h](stb_image_resize2.h)
* font text rasterizer: [stb_truetype.h](stb_truetype.h)
* typesafe containers: [stb_ds.h](stb_ds.h)
Most libraries by stb, except: stb_dxt by Fabian "ryg" Giesen, original stb_image_resize
by Jorge L. "VinoBS" Rodriguez, and stb_image_resize2 and stb_sprintf by Jeff Roberts.
<a name="stb_libs"></a>
library | lastest version | category | LoC | description
--------------------- | ---- | -------- | --- | --------------------------------
**[stb_vorbis.c](stb_vorbis.c)** | 1.22 | audio | 5584 | decode ogg vorbis files from file/memory to float/16-bit signed output
**[stb_hexwave.h](stb_hexwave.h)** | 0.5 | audio | 680 | audio waveform synthesizer
**[stb_image.h](stb_image.h)** | 2.29 | graphics | 7985 | image loading/decoding from file/memory: JPG, PNG, TGA, BMP, PSD, GIF, HDR, PIC
**[stb_truetype.h](stb_truetype.h)** | 1.26 | graphics | 5077 | parse, decode, and rasterize characters from truetype fonts
**[stb_image_write.h](stb_image_write.h)** | 1.16 | graphics | 1724 | image writing to disk: PNG, TGA, BMP
**[stb_image_resize2.h](stb_image_resize2.h)** | 2.07 | graphics | 10366 | resize images larger/smaller with good quality
**[stb_rect_pack.h](stb_rect_pack.h)** | 1.01 | graphics | 623 | simple 2D rectangle packer with decent quality
**[stb_perlin.h](stb_perlin.h)** | 0.5 | graphics | 428 | perlin's revised simplex noise w/ different seeds
**[stb_ds.h](stb_ds.h)** | 0.67 | utility | 1895 | typesafe dynamic array and hash tables for C, will compile in C++
**[stb_sprintf.h](stb_sprintf.h)** | 1.10 | utility | 1906 | fast sprintf, snprintf for C/C++
**[stb_textedit.h](stb_textedit.h)** | 1.14 | user&nbsp;interface | 1429 | guts of a text editor for games etc implementing them from scratch
**[stb_voxel_render.h](stb_voxel_render.h)** | 0.89 | 3D&nbsp;graphics | 3807 | Minecraft-esque voxel rendering "engine" with many more features
**[stb_dxt.h](stb_dxt.h)** | 1.12 | 3D&nbsp;graphics | 719 | Fabian "ryg" Giesen's real-time DXT compressor
**[stb_easy_font.h](stb_easy_font.h)** | 1.1 | 3D&nbsp;graphics | 305 | quick-and-dirty easy-to-deploy bitmap font for printing frame rate, etc
**[stb_tilemap_editor.h](stb_tilemap_editor.h)** | 0.42 | game&nbsp;dev | 4187 | embeddable tilemap editor
**[stb_herringbone_wa...](stb_herringbone_wang_tile.h)** | 0.7 | game&nbsp;dev | 1221 | herringbone Wang tile map generator
**[stb_c_lexer.h](stb_c_lexer.h)** | 0.12 | parsing | 940 | simplify writing parsers for C-like languages
**[stb_divide.h](stb_divide.h)** | 0.94 | math | 433 | more useful 32-bit modulus e.g. "euclidean divide"
**[stb_connected_comp...](stb_connected_components.h)** | 0.96 | misc | 1049 | incrementally compute reachability on grids
**[stb_leakcheck.h](stb_leakcheck.h)** | 0.6 | misc | 194 | quick-and-dirty malloc/free leak-checking
**[stb_include.h](stb_include.h)** | 0.02 | misc | 295 | implement recursive #include support, particularly for GLSL
Total libraries: 21
Total lines of C code: 50847
FAQ
---
#### What's the license?
These libraries are in the public domain. You can do anything you
want with them. You have no legal obligation
to do anything else, although I appreciate attribution.
They are also licensed under the MIT open source license, if you have lawyers
who are unhappy with public domain. Every source file includes an explicit
dual-license for you to choose from.
#### How do I use these libraries?
The idea behind single-header file libraries is that they're easy to distribute and deploy
because all the code is contained in a single file. By default, the .h files in here act as
their own header files, i.e. they declare the functions contained in the file but don't
actually result in any code getting compiled.
So in addition, you should select _exactly one_ C/C++ source file that actually instantiates
the code, preferably a file you're not editing frequently. This file should define a
specific macro (this is documented per-library) to actually enable the function definitions.
For example, to use stb_image, you should have exactly one C/C++ file that doesn't
include stb_image.h regularly, but instead does
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
The right macro to define is pointed out right at the top of each of these libraries.
#### <a name="other_libs"></a> Are there other single-file public-domain/open source libraries with minimal dependencies out there?
[Yes.](https://github.com/nothings/single_file_libs)
#### If I wrap an stb library in a new library, does the new library have to be public domain/MIT?
No, because it's public domain you can freely relicense it to whatever license your new
library wants to be.
#### What's the deal with SSE support in GCC-based compilers?
stb_image will either use SSE2 (if you compile with -msse2) or
will not use any SIMD at all, rather than trying to detect the
processor at runtime and handle it correctly. As I understand it,
the approved path in GCC for runtime-detection require
you to use multiple source files, one for each CPU configuration.
Because stb_image is a header-file library that compiles in only
one source file, there's no approved way to build both an
SSE-enabled and a non-SSE-enabled variation.
While we've tried to work around it, we've had multiple issues over
the years due to specific versions of gcc breaking what we're doing,
so we've given up on it. See https://github.com/nothings/stb/issues/280
and https://github.com/nothings/stb/issues/410 for examples.
#### Some of these libraries seem redundant to existing open source libraries. Are they better somehow?
Generally they're only better in that they're easier to integrate,
easier to use, and easier to release (single file; good API; no
attribution requirement). They may be less featureful, slower,
and/or use more memory. If you're already using an equivalent
library, there's probably no good reason to switch.
#### Can I link directly to the table of stb libraries?
You can use [this URL](https://github.com/nothings/stb#stb_libs) to link directly to that list.
#### Why do you list "lines of code"? It's a terrible metric.
Just to give you some idea of the internal complexity of the library,
to help you manage your expectations, or to let you know what you're
getting into. While not all the libraries are written in the same
style, they're certainly similar styles, and so comparisons between
the libraries are probably still meaningful.
Note though that the lines do include both the implementation, the
part that corresponds to a header file, and the documentation.
#### Why single-file headers?
Windows doesn't have standard directories where libraries
live. That makes deploying libraries in Windows a lot more
painful than open source developers on Unix-derivates generally
realize. (It also makes library dependencies a lot worse in Windows.)
There's also a common problem in Windows where a library was built
against a different version of the runtime library, which causes
link conflicts and confusion. Shipping the libs as headers means
you normally just compile them straight into your project without
making libraries, thus sidestepping that problem.
Making them a single file makes it very easy to just
drop them into a project that needs them. (Of course you can
still put them in a proper shared library tree if you want.)
Why not two files, one a header and one an implementation?
The difference between 10 files and 9 files is not a big deal,
but the difference between 2 files and 1 file is a big deal.
You don't need to zip or tar the files up, you don't have to
remember to attach *two* files, etc.
#### Why "stb"? Is this something to do with Set-Top Boxes?
No, they are just the initials for my name, Sean T. Barrett.
This was not chosen out of egomania, but as a moderately sane
way of namespacing the filenames and source function names.
#### Will you add more image types to stb_image.h?
No. As stb_image use has grown, it has become more important
for us to focus on security of the codebase. Adding new image
formats increases the amount of code we need to secure, so it
is no longer worth adding new formats.
#### Do you have any advice on how to create my own single-file library?
Yes. https://github.com/nothings/stb/blob/master/docs/stb_howto.txt
#### Why public domain?
I prefer it over GPL, LGPL, BSD, zlib, etc. for many reasons.
Some of them are listed here:
https://github.com/nothings/stb/blob/master/docs/why_public_domain.md
#### Why C?
Primarily, because I use C, not C++. But it does also make it easier
for other people to use them from other languages.
#### Why not C99? stdint.h, declare-anywhere, etc.
I still use MSVC 6 (1998) as my IDE because it has better human factors
for me than later versions of MSVC.

File diff suppressed because it is too large Load Diff

View File

@ -116,7 +116,7 @@ void SoundOptionsStart(void) {
DrawDial(0, 0);
DrawDial(1, 0);
MoveDialFromTo(0, 0, 4 * gProgram_state.music_volume);
MoveDialFromTo(1, 0, 4 * gProgram_state.music_volume);
MoveDialFromTo(1, 0, 4 * gProgram_state.effects_volume);
}
// IDA: int __usercall SoundOptionsDone@<EAX>(int pCurrent_choice@<EAX>, int pCurrent_mode@<EDX>, int pGo_ahead@<EBX>, int pEscaped@<ECX>, int pTimed_out)

View File

@ -16,13 +16,13 @@
#include "s3/s3_brender.h"
#include "utility.h"
int gSound_detail_level;
int gSound_detail_level = 1;
int gVirgin_pass = 1;
int gOld_sound_detail_level = -1;
int gLast_tune;
int gRandom_MIDI_tunes[3];
int gRandom_Rockin_MIDI_tunes[3];
int gRandom_CDA_tunes[7];
int gLast_tune = -1;
int gRandom_MIDI_tunes[3] = { 9500, 9501, 9502 };
int gRandom_Rockin_MIDI_tunes[3] = { 9500, 9501, 9502 };
int gRandom_CDA_tunes[8] = { 9600, 9601, 9602, 9603, 9604, 9605, 9606, 9607 }; /* dethrace: Changed to size 8 */
int gCDA_is_playing;
int gServicing_sound;
int gSong_repeat_count;
@ -394,7 +394,7 @@ void SoundService(void) {
gServicing_sound = 1;
gLast_sound_service = PDGetTotalTime();
if (gCDA_tag) {
if (!S3IsCDAPlaying2()) {
if (!S3IsCDAPlaying()) {
StopMusic();
StartMusic();
}
@ -681,19 +681,59 @@ int GetIndexFromOutlet(tS3_outlet_ptr pOutlet) {
// IDA: int __usercall DRS3StartCDA@<EAX>(tS3_sound_id pCDA_id@<EAX>)
int DRS3StartCDA(tS3_sound_id pCDA_id) {
LOG_TRACE("(%d)", pCDA_id);
NOT_IMPLEMENTED();
if (!gCD_is_disabled && gMusic_available) {
if (!gCDA_is_playing && !gCDA_tag) {
if (S3CDAEnabled()) {
S3StopOutletSound(gMusic_outlet);
if (gSound_enabled) {
if (gProgram_state.cockpit_on && gProgram_state.cockpit_image_index >= 0) {
S3Service(1, 0);
} else {
S3Service(0, 0);
}
if (pCDA_id == 9999) {
do {
pCDA_id = gRandom_CDA_tunes[IRandomBetween(0, 7)];
} while (pCDA_id == gLast_tune);
}
gLast_tune = pCDA_id;
gCDA_is_playing = DRS3StartSoundNoPiping(gMusic_outlet, pCDA_id);
gCDA_tag = gCDA_is_playing;
if (!gCDA_is_playing) {
gCD_is_disabled = 1;
S3DisableCDA();
}
gSong_repeat_count = 0;
}
}
}
}
return gCDA_tag;
}
// IDA: int __cdecl DRS3StopCDA()
int DRS3StopCDA(void) {
LOG_TRACE("()");
NOT_IMPLEMENTED();
if (gMusic_available && gCDA_tag != 0) {
S3StopSound(gCDA_tag);
gCDA_is_playing = 0;
gCDA_tag = 0;
}
return gCDA_tag;
}
// IDA: void __cdecl StartMusic()
void StartMusic(void) {
if (gCD_fully_installed) {
gCDA_tag = DRS3StartCDA(9999);
}
}
// IDA: void __cdecl StopMusic()
void StopMusic(void) {
if (gCD_fully_installed) {
DRS3StopCDA();
}
}

View File

@ -9,7 +9,7 @@ extern int gOld_sound_detail_level;
extern int gLast_tune;
extern int gRandom_MIDI_tunes[3];
extern int gRandom_Rockin_MIDI_tunes[3];
extern int gRandom_CDA_tunes[7];
extern int gRandom_CDA_tunes[8];
extern int gCDA_is_playing;
extern int gServicing_sound;
extern int gSong_repeat_count;

View File

@ -1,7 +1,7 @@
#include "3d.h"
#include "audio.h"
#include "harness/trace.h"
#include "resource.h"
#include "s3.h"
#include "s3sound.h"
#include <math.h>
#include <stdio.h>

View File

@ -2,9 +2,7 @@ add_library(s3 STATIC)
target_include_directories(s3
PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/include"
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
include
)
target_link_libraries(s3 PRIVATE brender SDL2::SDL2 harness compile_with_werror)
@ -34,8 +32,8 @@ endif()
target_sources(s3 PRIVATE
3d.c
3d.h
audio.c
audio.h
s3.c
s3.h
ioserv.c
ioserv.h
profile.c

View File

@ -57,8 +57,12 @@ int S3StopOutletSound(tS3_outlet_ptr pOutlet);
int S3SetOutletVolume(tS3_outlet_ptr pOutlet, tS3_volume pVolume);
void S3UpdateSoundSource(tS3_outlet_ptr outlet, tS3_sound_tag tag, tS3_sound_source_ptr src, float pMax_distance_squared, int pPeriod, tS3_repeats pAmbient_repeats, tS3_volume pVolume, int pPitch, tS3_speed pSpeed);
int S3IsCDAPlaying2(void);
int S3SetEffects(tS3_sample_filter* filter1, tS3_sample_filter* filter2);
// CDA
int S3CDAEnabled(void);
int S3IsCDAPlaying(void);
void S3EnableCDA(void);
void S3DisableCDA(void);
#endif

View File

@ -1,4 +1,4 @@
#include "audio.h"
#include "s3.h"
#include "resource.h"
#include "3d.h"
@ -10,6 +10,7 @@
#include "s3cda.h"
#include "s3music.h"
#include "s3sound.h"
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <math.h>
@ -1204,6 +1205,8 @@ char* S3GetCurrentDir(void) {
tS3_descriptor* S3GetDescriptorByID(tS3_sound_tag id) {
tS3_descriptor* d; // [esp+Ch] [ebp-4h]
assert(id != 0);
for (d = gS3_descriptors;; d = d->next) {
if (!d) {
return 0;

View File

@ -4,6 +4,7 @@
#include "brender.h"
#include "s3_defs.h"
extern tS3_outlet* gS3_outlets;
extern int gS3_enabled;
extern int gS3_last_error;
extern tS3_channel gS3_channel_template;

View File

@ -1,5 +1,7 @@
#include "s3cda.h"
#include "harness/audio.h"
#include "harness/trace.h"
#include "s3.h"
int gS3_cda_enabled;
@ -12,30 +14,54 @@ void S3DisableCDA(void) {
gS3_cda_enabled = 0;
}
void S3StopCDAOutlets(void) {
STUB();
int S3StopCDAOutlets(void) {
tS3_channel* chan;
tS3_outlet* o;
if (gS3_cda_enabled) {
for (o = gS3_outlets; o; o = o->next) {
for (chan = o->channel_list; chan; chan = chan->next) {
if (chan->type == eS3_ST_cda) {
AudioBackend_StopCDA();
// S3SetMCIStopCommand(chan);
}
}
}
}
return 1;
}
int S3CDAEnabled(void) {
return gS3_cda_enabled;
}
int S3PlayCDA(tS3_channel* chan) {
return 0;
int track;
if (gS3_cda_enabled) {
track = strtoul(chan->descriptor->filename, NULL, 10);
if (AudioBackend_PlayCDA(track) == eAB_error) {
return eS3_error_start_cda;
}
}
return eS3_error_none;
}
int S3StopCDA(tS3_channel* chan) {
return 0;
AudioBackend_StopCDA();
return eS3_error_none;
}
int S3SetCDAVolume(tS3_channel* chan, int pVolume) {
// if ( gS3_cda_enabled )
// {
// sub_49CC70();
// }
return 0;
}
int S3IsCDAPlaying(void) {
if (gS3_cda_enabled) {
AudioBackend_SetCDAVolume(pVolume);
}
return 0;
}
int S3IsCDAPlaying2(void) {
return S3IsCDAPlaying();
return AudioBackend_CDAIsPlaying();
}
int S3IsCDAPlaying(void) {
return S3IsCDAPlaying2();
}

View File

@ -5,7 +5,7 @@
void S3EnableCDA(void);
void S3DisableCDA(void);
void S3StopCDAOutlets(void);
int S3StopCDAOutlets(void);
int S3PlayCDA(tS3_channel* chan);
int S3StopCDA(tS3_channel* chan);

View File

@ -1,9 +1,9 @@
#include "s3sound.h"
#include "audio.h"
#include "harness/audio.h"
#include "harness/hooks.h"
#include "harness/trace.h"
#include "resource.h"
#include "s3.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

View File

@ -19,7 +19,7 @@ if(DETHRACE_FIX_BUGS)
target_compile_definitions(harness PRIVATE DETHRACE_FIX_BUGS)
endif()
target_link_libraries(harness PRIVATE brender miniaudio compile_with_werror)
target_link_libraries(harness PRIVATE brender miniaudio stb compile_with_werror)
if(NOT MSVC)
target_compile_options(harness PRIVATE

View File

@ -1,10 +1,22 @@
// Disable miniaudio's 'null' device fallback. A proper device must be found to enable playback
#define MA_NO_NULL
#include "miniaudio/miniaudio.h"
#include "harness/audio.h"
#include "harness/config.h"
#include "harness/os.h"
#include "harness/trace.h"
// Must come before miniaudio.h
#define STB_VORBIS_HEADER_ONLY
#include "stb/stb_vorbis.c"
#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio/miniaudio.h"
// Must come after miniaudio.h
#undef STB_VORBIS_HEADER_ONLY
#include "stb/stb_vorbis.c"
#include <assert.h>
#include <stdio.h>
#include <string.h>
@ -29,6 +41,7 @@ typedef struct tMiniaudio_stream {
} tMiniaudio_stream;
ma_engine engine;
ma_sound cda_sound;
tAudioBackend_error_code AudioBackend_Init(void) {
ma_result result;
@ -42,11 +55,16 @@ tAudioBackend_error_code AudioBackend_Init(void) {
}
LOG_INFO("Playback device: '%s'", engine.pDevice->playback.name);
ma_engine_set_volume(&engine, harness_game_config.volume_multiplier);
return eAB_success;
}
tAudioBackend_error_code AudioBackend_InitCDA(void) {
return eAB_error;
// check if music files are present or not
if (access("MUSIC/Track02.ogg", F_OK) == -1) {
return eAB_error;
}
return eAB_success;
}
void AudioBackend_UnInit(void) {
@ -56,6 +74,43 @@ void AudioBackend_UnInit(void) {
void AudioBackend_UnInitCDA(void) {
}
tAudioBackend_error_code AudioBackend_StopCDA(void) {
if (ma_sound_is_playing(&cda_sound)) {
ma_sound_stop(&cda_sound);
ma_sound_uninit(&cda_sound);
}
return eAB_success;
}
tAudioBackend_error_code AudioBackend_PlayCDA(int track) {
char path[256];
ma_result result;
sprintf(path, "MUSIC/Track0%d.ogg", track);
if (access(path, F_OK) == -1) {
return eAB_error;
}
result = ma_sound_init_from_file(&engine, path, 0, NULL, NULL, &cda_sound);
if (result != MA_SUCCESS) {
return eAB_error;
}
result = ma_sound_start(&cda_sound);
if (result != MA_SUCCESS) {
return eAB_error;
}
return eAB_success;
}
int AudioBackend_CDAIsPlaying(void) {
return ma_sound_is_playing(&cda_sound);
}
tAudioBackend_error_code AudioBackend_SetCDAVolume(int volume) {
ma_sound_set_volume(&cda_sound, volume / 255.0f);
return eAB_success;
}
void* AudioBackend_AllocateSampleTypeStruct(void) {
tMiniaudio_sample* sample_struct;
sample_struct = BrMemAllocate(sizeof(tMiniaudio_sample), kMem_S3_DOS_SOS_channel);

View File

@ -22,6 +22,11 @@ tAudioBackend_error_code AudioBackend_SetVolume(void* type_struct_sample, int vo
tAudioBackend_error_code AudioBackend_SetPan(void* type_struct_sample, int pan);
tAudioBackend_error_code AudioBackend_SetFrequency(void* type_struct_sample, int original_rate, int new_rate);
tAudioBackend_error_code AudioBackend_PlayCDA(int track);
tAudioBackend_error_code AudioBackend_StopCDA(void);
int AudioBackend_CDAIsPlaying(void);
tAudioBackend_error_code AudioBackend_SetCDAVolume(int volume);
// Used by smackw32
tAudioBackend_stream* AudioBackend_StreamOpen(int bitdepth, int channels, unsigned int sample_rate);
tAudioBackend_error_code AudioBackend_StreamWrite(tAudioBackend_stream* stream_handle, const unsigned char* data, unsigned long size);