| 1 | /* |
| 2 | * This source code is public domain. |
| 3 | * |
| 4 | * Authors: Kenton Varda <[email protected]> (C interface wrapper) |
| 5 | */ |
| 6 | |
| 7 | #include "stdafx.h" |
| 8 | #include "psmplug.h" |
| 9 | #include "sndfile.h" |
| 10 | |
| 11 | struct _ModPlugFile |
| 12 | { |
| 13 | CSoundFile mSoundFile; |
| 14 | }; |
| 15 | |
| 16 | namespace ModPlug |
| 17 | { |
| 18 | ModPlug_Settings gSettings = |
| 19 | { |
| 20 | MODPLUG_ENABLE_OVERSAMPLING | MODPLUG_ENABLE_NOISE_REDUCTION, |
| 21 | |
| 22 | 2, // mChannels |
| 23 | 16, // mBits |
| 24 | 44100, // mFrequency |
| 25 | MODPLUG_RESAMPLE_LINEAR, //mResamplingMode |
| 26 | |
| 27 | 128, // mStereoSeparation |
| 28 | 32, // mMaxMixChannels |
| 29 | 0, |
| 30 | 0, |
| 31 | 0, |
| 32 | 0, |
| 33 | 0, |
| 34 | 0, |
| 35 | 0 |
| 36 | }; |
| 37 | |
| 38 | int gSampleSize; |
| 39 | |
| 40 | void UpdateSettings(bool updateBasicConfig) |
| 41 | { |
| 42 | if(gSettings.mFlags & MODPLUG_ENABLE_REVERB) |
| 43 | { |
| 44 | CSoundFile::SetReverbParameters(gSettings.mReverbDepth, |
| 45 | gSettings.mReverbDelay); |
| 46 | } |
| 47 | |
| 48 | if(gSettings.mFlags & MODPLUG_ENABLE_MEGABASS) |
| 49 | { |
| 50 | CSoundFile::SetXBassParameters(gSettings.mBassAmount, |
| 51 | gSettings.mBassRange); |
| 52 | } |
| 53 | else // modplug seems to ignore the SetWaveConfigEx() setting for bass boost |
| 54 | CSoundFile::SetXBassParameters(0, 0); |
| 55 | |
| 56 | if(gSettings.mFlags & MODPLUG_ENABLE_SURROUND) |
| 57 | { |
| 58 | CSoundFile::SetSurroundParameters(gSettings.mSurroundDepth, |
| 59 | gSettings.mSurroundDelay); |
| 60 | } |
| 61 | |
| 62 | if(updateBasicConfig) |
| 63 | { |
| 64 | CSoundFile::SetWaveConfig(gSettings.mFrequency, |
| 65 | gSettings.mBits, |
| 66 | gSettings.mChannels); |
| 67 | CSoundFile::SetMixConfig(gSettings.mStereoSeparation, |
| 68 | gSettings.mMaxMixChannels); |
| 69 | |
| 70 | gSampleSize = gSettings.mBits / 8 * gSettings.mChannels; |
| 71 | } |
| 72 | |
| 73 | CSoundFile::SetWaveConfigEx(gSettings.mFlags & MODPLUG_ENABLE_SURROUND, |
| 74 | !(gSettings.mFlags & MODPLUG_ENABLE_OVERSAMPLING), |
| 75 | gSettings.mFlags & MODPLUG_ENABLE_REVERB, |
| 76 | true, |
| 77 | gSettings.mFlags & MODPLUG_ENABLE_MEGABASS, |
| 78 | gSettings.mFlags & MODPLUG_ENABLE_NOISE_REDUCTION, |
| 79 | false); |
| 80 | CSoundFile::SetResamplingMode(gSettings.mResamplingMode); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | ModPlugFile* ModPlug_Load(const void* data, int size) |
| 85 | { |
| 86 | ModPlugFile* result = new ModPlugFile; |
| 87 | ModPlug::UpdateSettings(true); |
| 88 | if(result->mSoundFile.Create((const BYTE*)data, size)) |
| 89 | { |
| 90 | result->mSoundFile.SetRepeatCount(ModPlug::gSettings.mLoopCount); |
| 91 | return result; |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | delete result; |
| 96 | return NULL; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void ModPlug_Unload(ModPlugFile* file) |
| 101 | { |
| 102 | file->mSoundFile.Destroy(); |
| 103 | delete file; |
| 104 | } |
| 105 | |
| 106 | int ModPlug_Read(ModPlugFile* file, void* buffer, int size) |
| 107 | { |
| 108 | return file->mSoundFile.Read(buffer, size) * ModPlug::gSampleSize; |
| 109 | } |
| 110 | |
| 111 | const char* ModPlug_GetName(ModPlugFile* file) |
| 112 | { |
| 113 | return file->mSoundFile.GetTitle(); |
| 114 | } |
| 115 | |
| 116 | int ModPlug_GetLength(ModPlugFile* file) |
| 117 | { |
| 118 | return file->mSoundFile.GetSongTime() * 1000; |
| 119 | } |
| 120 | |
| 121 | void ModPlug_InitMixerCallback(ModPlugFile* file,ModPlugMixerProc proc) |
| 122 | { |
| 123 | file->mSoundFile.gpSndMixHook = (LPSNDMIXHOOKPROC)proc ; |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | void ModPlug_UnloadMixerCallback(ModPlugFile* file) |
| 128 | { |
| 129 | file->mSoundFile.gpSndMixHook = NULL; |
| 130 | return ; |
| 131 | } |
| 132 | |
| 133 | unsigned int ModPlug_GetMasterVolume(ModPlugFile* file) |
| 134 | { |
| 135 | return (unsigned int)file->mSoundFile.m_nMasterVolume; |
| 136 | } |
| 137 | |
| 138 | void ModPlug_SetMasterVolume(ModPlugFile* file,unsigned int cvol) |
| 139 | { |
| 140 | (void)file->mSoundFile.SetMasterVolume( (UINT)cvol, |
| 141 | FALSE ); |
| 142 | return ; |
| 143 | } |
| 144 | |
| 145 | unsigned int ModPlug_GetMusicTempoFactor(ModPlugFile* file) |
| 146 | { |
| 147 | return file->mSoundFile.m_nTempoFactor; |
| 148 | } |
| 149 | |
| 150 | void ModPlug_SetMusicTempoFactor(ModPlugFile* file, unsigned int ctemp) |
| 151 | { |
| 152 | (void)file->mSoundFile.SetTempoFactor((UINT)ctemp); |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | int ModPlug_GetCurrentSpeed(ModPlugFile* file) |
| 157 | { |
| 158 | return file->mSoundFile.m_nMusicSpeed; |
| 159 | } |
| 160 | |
| 161 | int ModPlug_GetCurrentTempo(ModPlugFile* file) |
| 162 | { |
| 163 | return file->mSoundFile.m_nMusicTempo; |
| 164 | } |
| 165 | |
| 166 | int ModPlug_GetCurrentOrder(ModPlugFile* file) |
| 167 | { |
| 168 | return file->mSoundFile.GetCurrentOrder(); |
| 169 | } |
| 170 | |
| 171 | int ModPlug_GetCurrentPattern(ModPlugFile* file) |
| 172 | { |
| 173 | return file->mSoundFile.GetCurrentPattern(); |
| 174 | } |
| 175 | |
| 176 | int ModPlug_GetCurrentRow(ModPlugFile* file) |
| 177 | { |
| 178 | return file->mSoundFile.m_nRow; |
| 179 | } |
| 180 | |
| 181 | int ModPlug_GetPlayingChannels(ModPlugFile* file) |
| 182 | { |
| 183 | return ( file->mSoundFile.m_nMixChannels < file->mSoundFile.m_nMaxMixChannels ? file->mSoundFile.m_nMixChannels : file->mSoundFile.m_nMaxMixChannels ); |
| 184 | } |
| 185 | |
| 186 | void ModPlug_SeekOrder(ModPlugFile* file,int order) |
| 187 | { |
| 188 | file->mSoundFile.SetCurrentOrder(order); |
| 189 | } |
| 190 | |
| 191 | int ModPlug_GetModuleType(ModPlugFile* file) |
| 192 | { |
| 193 | return file->mSoundFile.m_nType; |
| 194 | } |
| 195 | |
| 196 | char* ModPlug_GetMessage(ModPlugFile* file) |
| 197 | { |
| 198 | return file->mSoundFile.m_lpszSongComments; |
| 199 | } |
| 200 | |
| 201 | unsigned int ModPlug_NumInstruments(ModPlugFile* file) |
| 202 | { |
| 203 | return file->mSoundFile.m_nInstruments; |
| 204 | } |
| 205 | |
| 206 | unsigned int ModPlug_NumSamples(ModPlugFile* file) |
| 207 | { |
| 208 | return file->mSoundFile.m_nSamples; |
| 209 | } |
| 210 | |
| 211 | unsigned int ModPlug_NumPatterns(ModPlugFile* file) |
| 212 | { |
| 213 | return file->mSoundFile.GetNumPatterns(); |
| 214 | } |
| 215 | |
| 216 | unsigned int ModPlug_NumChannels(ModPlugFile* file) |
| 217 | { |
| 218 | return file->mSoundFile.GetNumChannels(); |
| 219 | } |
| 220 | |
| 221 | unsigned int ModPlug_SampleName(ModPlugFile* file,unsigned int qual,char* buff) |
| 222 | { |
| 223 | return file->mSoundFile.GetSampleName(qual,buff); |
| 224 | } |
| 225 | |
| 226 | unsigned int ModPlug_InstrumentName(ModPlugFile* file,unsigned int qual,char* buff) |
| 227 | { |
| 228 | return file->mSoundFile.GetInstrumentName(qual,buff); |
| 229 | } |
| 230 | |
| 231 | ModPlugNote* ModPlug_GetPattern(ModPlugFile* file,int pattern,unsigned int* numrows) { |
| 232 | if ( pattern<MAX_PATTERNS && pattern >= 0) { |
| 233 | if (file->mSoundFile.Patterns[pattern]) { |
| 234 | if (numrows) *numrows=(unsigned int)file->mSoundFile.PatternSize[pattern]; |
| 235 | return (ModPlugNote*)file->mSoundFile.Patterns[pattern]; |
| 236 | } |
| 237 | } |
| 238 | return NULL; |
| 239 | } |
| 240 | |
| 241 | void ModPlug_Seek(ModPlugFile* file, int millisecond) |
| 242 | { |
| 243 | int maxpos; |
| 244 | int maxtime = file->mSoundFile.GetSongTime() * 1000; |
| 245 | float postime; |
| 246 | |
| 247 | if(millisecond > maxtime) |
| 248 | millisecond = maxtime; |
| 249 | maxpos = file->mSoundFile.GetMaxPosition(); |
| 250 | postime = 0.0f; |
| 251 | if (maxtime != 0) |
| 252 | postime = (float)maxpos / (float)maxtime; |
| 253 | |
| 254 | file->mSoundFile.SetCurrentPos((int)(millisecond * postime)); |
| 255 | } |
| 256 | |
| 257 | int ModPlug_Tell(ModPlugFile *file) |
| 258 | { |
| 259 | int currentPos = (int)file->mSoundFile.GetCurrentPos(); |
| 260 | int maxpos; |
| 261 | int maxtime = (int)file->mSoundFile.GetSongTime() * 1000; |
| 262 | float postime; |
| 263 | maxpos = (int)file->mSoundFile.GetMaxPosition(); |
| 264 | postime = 0.0f; |
| 265 | if (maxtime != 0.0f) |
| 266 | postime = (float)maxpos / (float)maxtime; |
| 267 | if (postime == 0.0f) |
| 268 | return 0; |
| 269 | return (int) ((float)currentPos / postime); |
| 270 | } |
| 271 | |
| 272 | void ModPlug_GetSettings(ModPlug_Settings* settings) |
| 273 | { |
| 274 | memcpy(settings, &ModPlug::gSettings, sizeof(ModPlug_Settings)); |
| 275 | } |
| 276 | |
| 277 | void ModPlug_SetSettings(const ModPlug_Settings* settings) |
| 278 | { |
| 279 | memcpy(&ModPlug::gSettings, settings, sizeof(ModPlug_Settings)); |
| 280 | ModPlug::UpdateSettings(false); // do not update basic config. |
| 281 | } |