Skip to content

Commit

Permalink
Rewrite mixer in pure C
Browse files Browse the repository at this point in the history
  • Loading branch information
jorio committed Feb 9, 2023
1 parent b09f9d2 commit 6159713
Show file tree
Hide file tree
Showing 9 changed files with 1,134 additions and 1,091 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ jobs:
run: |
cmake --install build --prefix build/install
copy build/ReadMe.txt build/Release
copy build/install/bin/msvcp140.dll build/Release
copy build/install/bin/vcruntime140.dll build/Release
copy build/install/bin/vcruntime140_1.dll build/Release
- name: Upload
uses: actions/upload-artifact@v3
Expand Down
60 changes: 18 additions & 42 deletions src/music.cpp → src/music.c
Original file line number Diff line number Diff line change
@@ -1,50 +1,38 @@
// music.c

#include <string.h>
#include <vector>
#include <fstream>

extern "C"
{

#include "main.h"
#include "music.h"
#include "gworld.h"
#include "gameticks.h"
#include "soundfx.h"
#include "graphics.h"
#include "support/cmixer.h"

}
#define k_noMusic (-1)
#define k_songs 14

#include "support/ModStream.h"

const int k_noMusic = -1;
const int k_songs = 14;

extern "C"
{
MBoolean musicOn = true;
int musicSelection = k_noMusic;

static MBoolean s_musicFast = false;
int s_musicPaused = 0;
}

static cmixer::ModStream* s_musicChannel = NULL;
static struct CMVoice* s_musicChannel = NULL;

void EnableMusic( MBoolean on )
{
if (s_musicChannel)
{
s_musicChannel->SetGain(on? 1.0: 0.0);
CMVoice_SetGain(s_musicChannel, on ? 1 : 0);
}
}

void FastMusic( void )
{
if (s_musicChannel && !s_musicFast)
{
s_musicChannel->SetPlaybackSpeed(1.3);
CMVoice_SetMODPlaybackSpeed(s_musicChannel, 1.3);
s_musicFast = true;
}
}
Expand All @@ -53,7 +41,7 @@ void SlowMusic( void )
{
if (s_musicChannel && s_musicFast)
{
s_musicChannel->SetPlaybackSpeed(1.0);
CMVoice_SetMODPlaybackSpeed(s_musicChannel, 1.0);
s_musicFast = false;
}
}
Expand All @@ -62,7 +50,7 @@ void PauseMusic( void )
{
if (s_musicChannel)
{
s_musicChannel->Pause();
CMVoice_Pause(s_musicChannel);
s_musicPaused++;
}
}
Expand All @@ -71,45 +59,34 @@ void ResumeMusic( void )
{
if (s_musicChannel)
{
s_musicChannel->Play();
CMVoice_Play(s_musicChannel);
s_musicPaused--;
}
}

static std::vector<char> LoadFile(char const* filename)
{
std::ifstream ifs(filename, std::ios::binary | std::ios::ate);
auto pos = ifs.tellg();
std::vector<char> bytes(pos);
ifs.seekg(0, std::ios::beg);
ifs.read(&bytes[0], pos);
return bytes;
}

void ChooseMusic( short which )
{
// Kill existing song first, if any
ShutdownMusic();

musicSelection = -1;

if (which >= 0 && which <= k_songs)
{
//printf("Music: %d\n" , which + 128);

auto qrn = QuickResourceName("mod", which+128, ".mod");
if (!FileExists(qrn)) {
const char* qrn = QuickResourceName("mod", which+128, ".mod");
if (!FileExists(qrn))
{
qrn = QuickResourceName("mod", which+128, ".s3m");
}
if (!FileExists(qrn)) {
if (!FileExists(qrn))
{
return;
}
auto rawFileData = LoadFile(qrn);

s_musicChannel = new cmixer::ModStream(LoadFile(qrn));
s_musicChannel = CMVoice_LoadMOD(qrn);

EnableMusic(musicOn);
s_musicChannel->Play();
CMVoice_Play(s_musicChannel);

musicSelection = which;
s_musicPaused = 0;
Expand All @@ -120,8 +97,7 @@ void ShutdownMusic()
{
if (s_musicChannel)
{
s_musicChannel->RemoveFromMixer();
delete s_musicChannel;
CMVoice_Free(s_musicChannel);
s_musicChannel = NULL;
}
}
81 changes: 81 additions & 0 deletions src/soundfx.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// soundfx.c

#include "support/cmixer.h"
#include <stdio.h>

#include "main.h"
#include "soundfx.h"
#include "music.h"

MBoolean soundOn = true;

#define k_playerStereoSeparation (0.5f)
#define k_soundEffectGain (0.7f)
static CMVoicePtr s_soundBank[kNumSounds];

void InitSound()
{
cmixer_InitWithSDL();

for (int i = 0; i < kNumSounds; i++)
{
const char* path = QuickResourceName("snd", i+128, ".wav");
if (!FileExists(path))
{
Error(path);
}

s_soundBank[i] = CMVoice_LoadWAV(path);
CMVoice_SetInterpolation(s_soundBank[i], true);
}
}

void ShutdownSound()
{
for (int i = 0; i < kNumSounds; i++)
{
CMVoice_Free(s_soundBank[i]);
s_soundBank[i] = NULL;
}

cmixer_ShutdownWithSDL();
}

void PlayMono( short which )
{
PlayStereoFrequency(2, which, 0);
}

void PlayStereo( short player, short which )
{
PlayStereoFrequency(player, which, 0);
}

void PlayStereoFrequency( short player, short which, short freq )
{
if (soundOn)
{
CMVoicePtr effect = s_soundBank[which];

double pan;
switch (player)
{
case 0: pan = -k_playerStereoSeparation; break;
case 1: pan = +k_playerStereoSeparation; break;
default: pan = 0.0; break;
}

//CMVoice_Stop(effect);
CMVoice_Rewind(effect);
CMVoice_SetGain(effect, k_soundEffectGain);
CMVoice_SetPan(effect, pan);
CMVoice_SetPitch(effect, 1.0 + freq/16.0);
CMVoice_Play(effect);

UpdateSound();
}
}

void UpdateSound()
{
}
82 changes: 0 additions & 82 deletions src/soundfx.cpp

This file was deleted.

61 changes: 0 additions & 61 deletions src/support/ModStream.cpp

This file was deleted.

Loading

0 comments on commit 6159713

Please sign in to comment.