Skip to content

Commit

Permalink
Moving music functions to separate file.
Browse files Browse the repository at this point in the history
  • Loading branch information
MainMemory committed Nov 27, 2017
1 parent d7324bf commit bb869d9
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 124 deletions.
128 changes: 128 additions & 0 deletions SonicRModLoader/Music.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#include "stdafx.h"
#include <MMSystem.h>
#include "SonicRModLoader.h"
#include "FileReplacement.h"
#include "bass_vgmstream.h"
#include "Music.h"

/**
* Callback for BASS. Called when it needs more data.
*/
DWORD CALLBACK rawStreamProc(HSTREAM handle, void *buffer, DWORD length, void *user)
{
FILE *file = (FILE*)user;
DWORD c = fread(buffer, 1, length, file); // read the file into the buffer
if (feof(file)) c |= BASS_STREAMPROC_END; // end of the file/stream
return c;
}

/**
* Called when the BASS handle is closed
*/
void CALLBACK rawStreamOnFree(HSYNC handle, DWORD channel, DWORD data, void *user)
{
fclose((FILE*)user);
}

bool bassinit = false;
void InitMusic()
{
bassinit = BASS_Init(-1, 44100, 0, nullptr, nullptr) ? true : false;
}

void DeInitMusic()
{
if (bassinit)
{
BASS_Free();
bassinit = false;
}
}

int GetCurrentTrackNumber()
{
return CurrentMusicTrack;
}

static void __stdcall onTrackEnd(HSYNC handle, DWORD channel, DWORD data, void *user)
{
BASS_ChannelStop(channel);
BASS_StreamFree(channel);
CurrentMusicTrack = -1;
*(int*)0x502F0C = 0;
}

float volumelevels[] = { 0, 0.002511886f, 0.007079458f, 0.031622777f, 0.079432823f, 0.125892541f, 0.354813389f, 0.630957344f, 1 };
int basschan = 0;
int wasPaused = 0;
DataPointer(int, IsGamePaused, 0x7C1BC0);
void PlayMusic(int track)
{
if (!bassinit) return;
if (basschan != 0)
{
BASS_ChannelStop(basschan);
BASS_StreamFree(basschan);
}
char buf[MAX_PATH];
snprintf(buf, sizeof(buf), "music\\track%u.son", track);
const char *filename = fileMap.replaceFile(buf);
basschan = BASS_VGMSTREAM_StreamCreate(filename, 0);
if (basschan == 0)
basschan = BASS_StreamCreateFile(false, filename, 0, 0, 0);
if (basschan == 0)
{
FILE *file = fopen(filename, "rb");
if (file)
{
basschan = BASS_StreamCreate(44100, 2, 0, rawStreamProc, file);
if (basschan != 0)
BASS_ChannelSetSync(basschan, BASS_SYNC_FREE | BASS_SYNC_MIXTIME, 0, rawStreamOnFree, file);
}
}
if (basschan != 0)
{
// Stream opened!
BASS_ChannelPlay(basschan, false);
BASS_ChannelSetAttribute(basschan, BASS_ATTRIB_VOL, volumelevels[MusicVolume]);
BASS_ChannelSetSync(basschan, BASS_SYNC_END, 0, onTrackEnd, nullptr);
CurrentMusicTrack = track;
*(int*)0x502F0C = 1;
wasPaused = IsGamePaused = 0;
}
else
{
CurrentMusicTrack = -1;
*(int*)0x502F0C = 0;
PrintDebug("Could not play music file \"%s\".", filename);
}
}

void StopMusic_r()
{
if (basschan != 0)
{
BASS_ChannelStop(basschan);
BASS_StreamFree(basschan);
CurrentMusicTrack = -1;
*(int*)0x502F0C = 0;
}
}

void UpdateMusicVolume_r()
{
if (basschan != 0)
BASS_ChannelSetAttribute(basschan, BASS_ATTRIB_VOL, volumelevels[MusicVolume]);
}

void CheckPauseMusic()
{
if (basschan != 0)
{
if (wasPaused && !IsGamePaused)
BASS_ChannelPlay(basschan, false);
else if (!wasPaused && IsGamePaused)
BASS_ChannelPause(basschan);
wasPaused = IsGamePaused;
}
}
9 changes: 9 additions & 0 deletions SonicRModLoader/Music.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

void InitMusic();
void DeInitMusic();
int GetCurrentTrackNumber();
void PlayMusic(int track);
void StopMusic_r();
void UpdateMusicVolume_r();
void CheckPauseMusic();
2 changes: 2 additions & 0 deletions SonicRModLoader/SonicRModLoader.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
<ClInclude Include="git.h" />
<ClInclude Include="include\SonicRModLoader.h" />
<ClInclude Include="include\MemAccess.h" />
<ClInclude Include="Music.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
</ItemGroup>
Expand All @@ -109,6 +110,7 @@
<ClCompile Include="FileMap.cpp" />
<ClCompile Include="FileReplacement.cpp" />
<ClCompile Include="FileSystem.cpp" />
<ClCompile Include="Music.cpp" />
<ClCompile Include="SonicRModLoader.cpp" />
<ClCompile Include="stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
Expand Down
6 changes: 6 additions & 0 deletions SonicRModLoader/SonicRModLoader.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
<ClInclude Include="git.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Music.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="stdafx.cpp">
Expand All @@ -68,5 +71,8 @@
<ClCompile Include="FileReplacement.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Music.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
125 changes: 1 addition & 124 deletions SonicRModLoader/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#include "FileSystem.h"
#include "FileReplacement.h"
#include "Events.h"
#include "Music.h"
#include "SonicRModLoader.h"
#include "bass_vgmstream.h"

using std::ifstream;
using std::string;
Expand Down Expand Up @@ -82,7 +82,6 @@ static void HookCreateFileA(void)
}
}

void CheckPauseMusic();
// Code Parser.
static CodeParser codeParser;

Expand Down Expand Up @@ -144,128 +143,6 @@ static int __cdecl SonicRDebugOutput(const char *Format, ...)
return result;
}

/**
* Callback for BASS. Called when it needs more data.
*/
DWORD CALLBACK rawStreamProc(HSTREAM handle, void *buffer, DWORD length, void *user)
{
FILE *file = (FILE*)user;
DWORD c = fread(buffer, 1, length, file); // read the file into the buffer
if (feof(file)) c |= BASS_STREAMPROC_END; // end of the file/stream
return c;
}

/**
* Called when the BASS handle is closed
*/
void CALLBACK rawStreamOnFree(HSYNC handle, DWORD channel, DWORD data, void *user)
{
fclose((FILE*)user);
}

bool bassinit = false;
void InitMusic()
{
bassinit = BASS_Init(-1, 44100, 0, nullptr, nullptr) ? true : false;
}

void DeInitMusic()
{
if (bassinit)
{
BASS_Free();
bassinit = false;
}
}

int GetCurrentTrackNumber()
{
return CurrentMusicTrack;
}

static void __stdcall onTrackEnd(HSYNC handle, DWORD channel, DWORD data, void *user)
{
BASS_ChannelStop(channel);
BASS_StreamFree(channel);
CurrentMusicTrack = -1;
*(int*)0x502F0C = 0;
}

float volumelevels[] = { 0, 0.002511886f, 0.007079458f, 0.031622777f, 0.079432823f, 0.125892541f, 0.354813389f, 0.630957344f, 1 };
int basschan = 0;
int wasPaused = 0;
DataPointer(int, IsGamePaused, 0x7C1BC0);
void PlayMusic(int track)
{
if (!bassinit) return;
if (basschan != 0)
{
BASS_ChannelStop(basschan);
BASS_StreamFree(basschan);
}
char buf[MAX_PATH];
snprintf(buf, sizeof(buf), "music\\track%u.son", track);
const char *filename = fileMap.replaceFile(buf);
basschan = BASS_VGMSTREAM_StreamCreate(filename, 0);
if (basschan == 0)
basschan = BASS_StreamCreateFile(false, filename, 0, 0, 0);
if (basschan == 0)
{
FILE *file = fopen(filename, "rb");
if (file)
{
basschan = BASS_StreamCreate(44100, 2, 0, rawStreamProc, file);
if (basschan != 0)
BASS_ChannelSetSync(basschan, BASS_SYNC_FREE | BASS_SYNC_MIXTIME, 0, rawStreamOnFree, file);
}
}
if (basschan != 0)
{
// Stream opened!
BASS_ChannelPlay(basschan, false);
BASS_ChannelSetAttribute(basschan, BASS_ATTRIB_VOL, volumelevels[MusicVolume]);
BASS_ChannelSetSync(basschan, BASS_SYNC_END, 0, onTrackEnd, nullptr);
CurrentMusicTrack = track;
*(int*)0x502F0C = 1;
wasPaused = IsGamePaused = 0;
}
else
{
CurrentMusicTrack = -1;
*(int*)0x502F0C = 0;
PrintDebug("Could not play music file \"%s\".", filename);
}
}

void StopMusic_r()
{
if (basschan != 0)
{
BASS_ChannelStop(basschan);
BASS_StreamFree(basschan);
CurrentMusicTrack = -1;
*(int*)0x502F0C = 0;
}
}

void UpdateMusicVolume_r()
{
if (basschan != 0)
BASS_ChannelSetAttribute(basschan, BASS_ATTRIB_VOL, volumelevels[MusicVolume]);
}

void CheckPauseMusic()
{
if (basschan != 0)
{
if (wasPaused && !IsGamePaused)
BASS_ChannelPlay(basschan, false);
else if (!wasPaused && IsGamePaused)
BASS_ChannelPause(basschan);
wasPaused = IsGamePaused;
}
}

static Gdiplus::Bitmap* backgroundImage = nullptr;
enum windowmodes { windowed, fullscreen };
struct windowsize { int x; int y; int width; int height; };
Expand Down

0 comments on commit bb869d9

Please sign in to comment.