CAudioGalaxy.cpp 16.2 KB
Newer Older
1 2 3 4 5 6 7
/*
 * CAudioGalaxy.cpp
 *
 *  Created on: 23.01.2011
 *      Author: gerstrong
 */

8
#include <fstream>
9
#include "CAudioGalaxy.h"
10
#include <lib/base/GsLogging.h>
Gerhard Stein's avatar
Gerhard Stein committed
11
#include <base/FindFile.h>
12
#include "fileio/ResourceMgmt.h"
13 14
#include "sdl/sound/CSound.h"
#include "fileio/compression/CHuffman.h"
15
#include "common/CBehaviorEngine.h"
16 17 18 19 20 21

CAudioGalaxy::CAudioGalaxy(const CExeFile &ExeFile, const SDL_AudioSpec &AudioSpec) :
CAudioResources(AudioSpec),
m_ExeFile(ExeFile)
{}

22
/**
23
 * Caution: This is Galaxy only and will be replaced some time
24 25
 * This function loads the PC Speaker sounds to CG (Galaxy Version, similar to Vorticon Version but not equal.)
 */
Gerhard Stein's avatar
Gerhard Stein committed
26
bool CAudioGalaxy::readPCSpeakerSoundintoWaveForm(CSoundSlot &soundslot, const byte *pcsdata, const unsigned int bytesize, const Uint8 formatsize)
27 28 29 30 31 32
{
	byte *pcsdata_ptr = (byte*)pcsdata;
	const longword size = READLONGWORD(pcsdata_ptr);
	soundslot.priority = READWORD(pcsdata_ptr);
	soundslot.setupAudioSpec(&m_AudioSpec);

33
	std::vector<Sint16> waveform;
34
	// TODO:  There should be a better way of determining if sound is signed or not...
35 36 37 38 39
	int AMP;
	if ((m_AudioSpec.format == AUDIO_S8) || (m_AudioSpec.format == AUDIO_S16))
		AMP = ((((1<<(formatsize*8))>>2)-1)*PC_Speaker_Volume)/100;
	else
		AMP = ((((1<<(formatsize*8))>>1)-1)*PC_Speaker_Volume)/100;
Gerhard Stein's avatar
Gerhard Stein committed
40

41
	generateWave(waveform, pcsdata_ptr, size, false, AMP);
42

Gerhard Stein's avatar
Gerhard Stein committed
43 44 45 46 47
	if(formatsize == 1)
	{
		std::vector<Uint8> wave8;
		std::vector<Sint16>::iterator it = waveform.begin();
		for( ; it != waveform.end(); it++ )
48
			wave8.push_back(*it);
Gerhard Stein's avatar
Gerhard Stein committed
49
		soundslot.setupWaveForm((Uint8*)&wave8[0], wave8.size()*sizeof(Uint8));
50
	}
Gerhard Stein's avatar
Gerhard Stein committed
51 52
	else
		soundslot.setupWaveForm((Uint8*)&waveform[0], waveform.size()*sizeof(Sint16));
53 54 55 56 57


	return true;
}

58 59 60 61 62

/**
 *  \brief Sets up the Map assignments for the Sounds used in Keen Galaxy
 */
void CAudioGalaxy::setupAudioMap()
gerstrong's avatar
gerstrong committed
63
{
64 65 66
    // Preparation which might help wehn some patches are applied
    byte *ptr = g_pBehaviorEngine->m_ExeFile.getRawData();
    auto episode = g_pBehaviorEngine->getEpisode();
gerstrong's avatar
gerstrong committed
67 68 69
    size_t holder = 0;

    // Episode 4
70 71 72 73
    sndSlotMapGalaxy[4][SOUND_KEEN_WALK] = 0;
    sndSlotMapGalaxy[4][SOUND_KEEN_WALK2] = 1;
    sndSlotMapGalaxy[4][SOUND_KEEN_JUMP] = 2;
    sndSlotMapGalaxy[4][SOUND_KEEN_LAND] = 3;
gerstrong's avatar
gerstrong committed
74
    sndSlotMapGalaxy[4][SOUND_KEEN_FIRE] = 4;
75
    sndSlotMapGalaxy[4][SOUND_WORMOUTH_STRIKE] = 5;
Gerhard Stein's avatar
Gerhard Stein committed
76
    // sndSlotMapGalaxy[4][?] = 6; // This sounds like alternative breathe under water. not sure
77 78 79 80 81 82 83
    sndSlotMapGalaxy[4][SOUND_KEEN_POGO] = 7;
    sndSlotMapGalaxy[4][SOUND_GET_BONUS] = 8;
    sndSlotMapGalaxy[4][SOUND_GET_AMMO] = 9;
    sndSlotMapGalaxy[4][SOUND_GET_DROP] = 10;
    sndSlotMapGalaxy[4][SOUND_GET_ITEM] = 11;
    sndSlotMapGalaxy[4][SOUND_ENTER_LEVEL] = 12;
    sndSlotMapGalaxy[4][SOUND_LEVEL_DONE] = 13;
Gerhard Stein's avatar
Gerhard Stein committed
84
    sndSlotMapGalaxy[4][SOUND_CANT_DO] = 14;
85 86 87
    sndSlotMapGalaxy[4][SOUND_KEEN_BUMPHEAD] = 15;
    sndSlotMapGalaxy[4][SOUND_BOUNCE_HIGH] = 16;
    sndSlotMapGalaxy[4][SOUND_EXTRA_LIFE] = 17;
gerstrong's avatar
gerstrong committed
88
    sndSlotMapGalaxy[4][SOUND_DOOR_OPEN] = 18;
89 90
    sndSlotMapGalaxy[4][SOUND_GET_GEM] = 19;
    sndSlotMapGalaxy[4][SOUND_KEEN_FALL] = 20;
gerstrong's avatar
gerstrong committed
91
    sndSlotMapGalaxy[4][SOUND_GUN_CLICK] = 21;
92 93 94 95
    sndSlotMapGalaxy[4][SOUND_SQUISH_SKYPEST] = 22;
    sndSlotMapGalaxy[4][SOUND_KEEN_DIE] = 23;
    //sndSlotMapGalaxy[4][?] = 24;
    sndSlotMapGalaxy[4][SOUND_SHOT_HIT] = 25;
gerstrong's avatar
gerstrong committed
96
    sndSlotMapGalaxy[4][SOUND_KEEN_SWIM] = 26;
97 98 99 100 101 102 103 104 105 106 107 108 109
    sndSlotMapGalaxy[4][SOUND_KEEN_SWIM_TO_LAND] = 27;
    sndSlotMapGalaxy[4][SOUND_BOUNCE_LOW] = 28;
    //sndSlotMapGalaxy[4][?] = 29;
    sndSlotMapGalaxy[4][SOUND_TRESURE_STEALER_TELEPORT] = 30;
    sndSlotMapGalaxy[4][SOUND_RESCUE_COUNCIL_MEMBER] = 31;
    sndSlotMapGalaxy[4][SOUND_LICK_FIREBREATH] = 32;
    sndSlotMapGalaxy[4][SOUND_BERKELOID_WINDUP] = 33;
    sndSlotMapGalaxy[4][SOUND_STATUS_SLIDE_IN] = 34;
    sndSlotMapGalaxy[4][SOUND_STATUS_SLIDE_OUT] = 35;
    sndSlotMapGalaxy[4][SOUND_BUBBLE] = 36;
    sndSlotMapGalaxy[4][SOUND_MINE_EXPLOSION] = 37;
    sndSlotMapGalaxy[4][SOUND_SPRITE_SHOT] = 38;
    sndSlotMapGalaxy[4][SOUND_THUNDERCLOUD_STRIKE] = 39;
gerstrong's avatar
gerstrong committed
110
    sndSlotMapGalaxy[4][SOUND_BERKELOID_FIREBALL_LANDING] = 40;
111
    sndSlotMapGalaxy[4][SOUND_DARTGUN_SHOOT] = 41;
gerstrong's avatar
gerstrong committed
112
    sndSlotMapGalaxy[4][SOUND_DOPEFISH_BURP] = 42;
113
    sndSlotMapGalaxy[4][SOUND_FLAG_APPEAR] = 43;
gerstrong's avatar
gerstrong committed
114 115
    sndSlotMapGalaxy[4][SOUND_FLAG_LAND] = 44;
    sndSlotMapGalaxy[4][SOUND_GET_WETSUIT] = 45;
116 117 118 119 120 121
    sndSlotMapGalaxy[4][SOUND_SLUG_DEFECATE] = 46;
    sndSlotMapGalaxy[4][SOUND_PLAYER_PADDLE] = 47;
    sndSlotMapGalaxy[4][SOUND_COMPUTER_PADDLE] = 48;
    sndSlotMapGalaxy[4][SOUND_HIT_SIDEWALL] = 49;
    sndSlotMapGalaxy[4][SOUND_COMPUTER_POINT] = 50;
    sndSlotMapGalaxy[4][SOUND_PLAYER_POINT] = 51;
gerstrong's avatar
gerstrong committed
122 123 124 125 126



    if(episode == 5) // Episode 5
    {
127 128 129
      sndSlotMapGalaxy[5][SOUND_KEEN_WALK] = 0;
      sndSlotMapGalaxy[5][SOUND_KEEN_WALK2] = 1;
      sndSlotMapGalaxy[5][SOUND_KEEN_JUMP] = 2;
gerstrong's avatar
gerstrong committed
130

131 132
      memcpy(&holder, ptr + 0xC87D, 1 );
      sndSlotMapGalaxy[5][SOUND_KEEN_LAND] = holder;
gerstrong's avatar
gerstrong committed
133 134

      sndSlotMapGalaxy[5][SOUND_KEEN_FIRE] = 4;
135 136 137 138 139 140 141 142 143 144 145 146 147
      sndSlotMapGalaxy[5][SOUND_MINEEXPLODE] = 5;
      sndSlotMapGalaxy[5][SOUND_SLICEBUMP] = 6;
      sndSlotMapGalaxy[5][SOUND_KEEN_POGO] = 7;
      sndSlotMapGalaxy[5][SOUND_GET_BONUS] = 8;
      sndSlotMapGalaxy[5][SOUND_GET_AMMO] = 9;
      sndSlotMapGalaxy[5][SOUND_GET_DROP] = 10;
      sndSlotMapGalaxy[5][SOUND_GET_ITEM] = 11;
      sndSlotMapGalaxy[5][SOUND_ENTER_LEVEL] = 12;
      sndSlotMapGalaxy[5][SOUND_LEVEL_DONE] = 13;
      sndSlotMapGalaxy[5][SOUND_CANT_DO] = 14;
      sndSlotMapGalaxy[5][SOUND_KEEN_BUMPHEAD] = 15;
      sndSlotMapGalaxy[5][SOUND_SPINDREDFLYUP] = 16;
      sndSlotMapGalaxy[5][SOUND_EXTRA_LIFE] = 17;
gerstrong's avatar
gerstrong committed
148
      sndSlotMapGalaxy[5][SOUND_OPEN_EXIT_DOOR] = 18;
149 150
      sndSlotMapGalaxy[5][SOUND_GET_GEM] = 19;
      sndSlotMapGalaxy[5][SOUND_KEEN_FALL] = 20;
gerstrong's avatar
gerstrong committed
151
      sndSlotMapGalaxy[5][SOUND_GUN_CLICK] = 21;
152 153 154 155
      //sndSlotMapGalaxy[5][?] = 22;
      sndSlotMapGalaxy[5][SOUND_KEEN_DIE] = 23;
      //sndSlotMapGalaxy[5][?] = 24;
      sndSlotMapGalaxy[5][SOUND_SHOT_HIT] = 25;
gerstrong's avatar
gerstrong committed
156
      //sndSlotMapGalaxy[5][?] = 26;
157
      sndSlotMapGalaxy[5][SOUND_SPIROGRIP] = 27;
Gerhard Stein's avatar
Gerhard Stein committed
158 159 160

      memcpy(&holder, ptr + 0x129FC, 1 );
      sndSlotMapGalaxy[5][SOUND_SPINDREDSLAM] = 28;
gerstrong's avatar
gerstrong committed
161

162 163 164 165 166 167 168 169
      sndSlotMapGalaxy[5][SOUND_ROBORED_SHOOT] = 29;
      //sndSlotMapGalaxy[5][SOUND_ROBOSHOTHIT] = 30;
      sndSlotMapGalaxy[5][SOUND_AMPTONWALK0] = 31;
      sndSlotMapGalaxy[5][SOUND_AMPTONWALK1] = 32;
      sndSlotMapGalaxy[5][SOUND_ROBO_STUN] = 33;
      sndSlotMapGalaxy[5][SOUND_STATUS_SLIDE_IN] = 34;
      sndSlotMapGalaxy[5][SOUND_STATUS_SLIDE_OUT] = 35;
      sndSlotMapGalaxy[5][SOUND_SPARKY_CHARGE] = 36;
gerstrong's avatar
gerstrong committed
170

Gerhard Stein's avatar
Gerhard Stein committed
171 172
      memcpy(&holder, ptr + 0x10F93, 1 );
      sndSlotMapGalaxy[5][SOUND_SPINDREDFLYDOWN] = holder;
gerstrong's avatar
gerstrong committed
173

174 175
      sndSlotMapGalaxy[5][SOUND_MASTERSHOT] = 38;
      sndSlotMapGalaxy[5][SOUND_MASTERTELE] = 39;
gerstrong's avatar
gerstrong committed
176
      sndSlotMapGalaxy[5][SOUND_POLEZAP] = 40;
177
      sndSlotMapGalaxy[5][SOUND_TELEPORT] = 41;
gerstrong's avatar
gerstrong committed
178
      sndSlotMapGalaxy[5][SOUND_SHOCKSUNDBARK] = 42;
179
      sndSlotMapGalaxy[5][SOUND_FLAG_APPEAR] = 43;
gerstrong's avatar
gerstrong committed
180 181
      sndSlotMapGalaxy[5][SOUND_FLAG_LAND] = 44;
      //sndSlotMapGalaxy[5][ACTION_BARKSHOTDIE0] = 45;
182 183 184 185 186 187 188 189 190 191
      //sndSlotMapGalaxy[5][SOUND_PLAYER_PADDLE] = 46;
      sndSlotMapGalaxy[5][SOUND_COMPUTER_PADDLE] = 47;
      sndSlotMapGalaxy[5][SOUND_HIT_SIDEWALL] = 48;
      sndSlotMapGalaxy[5][SOUND_COMPUTER_POINT] = 49;
      sndSlotMapGalaxy[5][SOUND_PLAYER_POINT] = 50;
      //sndSlotMapGalaxy[5][?] = 51;
      //sndSlotMapGalaxy[5][?] = 52;
      //sndSlotMapGalaxy[5][?] = 53;
      //sndSlotMapGalaxy[5][?] = 54;
      sndSlotMapGalaxy[5][SOUND_GET_CARD] = 55;
gerstrong's avatar
gerstrong committed
192

Gerhard Stein's avatar
Gerhard Stein committed
193 194
      memcpy(&holder, ptr + 0x13784, 1 );
      sndSlotMapGalaxy[5][SOUND_ELEVATING] = holder;
195 196 197
      //sndSlotMapGalaxy[5][SOUND_ELEVATOR_OPEN] = 57;
      //sndSlotMapGalaxy[5][?] = 58;
      sndSlotMapGalaxy[5][SOUND_SPHEREFULCEILING] = 59;
gerstrong's avatar
gerstrong committed
198

199 200 201 202 203
      sndSlotMapGalaxy[5][SOUND_DOOR_OPEN] = 60;
      sndSlotMapGalaxy[5][SOUND_SPIROFLY] = 61;
      //sndSlotMapGalaxy[5][?] = 62;
      sndSlotMapGalaxy[5][SOUND_ELEVATOR_OPEN] = 63;
    }
gerstrong's avatar
gerstrong committed
204 205

    // Episode 6
206 207 208 209
    sndSlotMapGalaxy[6][SOUND_KEEN_WALK] = 0;
    sndSlotMapGalaxy[6][SOUND_KEEN_WALK2] = 1;
    sndSlotMapGalaxy[6][SOUND_KEEN_JUMP] = 2;
    sndSlotMapGalaxy[6][SOUND_KEEN_LAND] = 3;
gerstrong's avatar
gerstrong committed
210
    sndSlotMapGalaxy[6][SOUND_KEEN_FIRE] = 4;
211
    sndSlotMapGalaxy[6][SOUND_JUMPED_GEM] = 5;
212
    sndSlotMapGalaxy[6][SOUND_KEEN_LAND_ALT] = 6;     
213 214 215 216 217 218 219 220 221 222 223
    sndSlotMapGalaxy[6][SOUND_KEEN_POGO] = 7;
    sndSlotMapGalaxy[6][SOUND_GET_BONUS] = 8;
    sndSlotMapGalaxy[6][SOUND_GET_AMMO] = 9;
    sndSlotMapGalaxy[6][SOUND_GET_DROP] = 10;
    sndSlotMapGalaxy[6][SOUND_GET_ITEM] = 11;
    sndSlotMapGalaxy[6][SOUND_ENTER_LEVEL] = 12;
    sndSlotMapGalaxy[6][SOUND_LEVEL_DONE] = 13;
    sndSlotMapGalaxy[6][SOUND_CANT_DO] = 14;
    sndSlotMapGalaxy[6][SOUND_KEEN_BUMPHEAD] = 15;
    sndSlotMapGalaxy[6][SOUND_BOUNCE_HIGH] = 16;
    sndSlotMapGalaxy[6][SOUND_EXTRA_LIFE] = 17;
gerstrong's avatar
gerstrong committed
224
    sndSlotMapGalaxy[6][SOUND_DOOR_OPEN] = 18;
225 226
    sndSlotMapGalaxy[6][SOUND_GET_GEM] = 19;
    sndSlotMapGalaxy[6][SOUND_KEEN_FALL] = 20;
gerstrong's avatar
gerstrong committed
227
    sndSlotMapGalaxy[6][SOUND_GUN_CLICK] = 21;
228
    sndSlotMapGalaxy[6][SOUND_SQUISH] = 22;
229
    sndSlotMapGalaxy[6][SOUND_KEEN_DIE] = 23;
230
    sndSlotMapGalaxy[6][SOUND_BIP_SHIP_CRASH] = 24;
231
    sndSlotMapGalaxy[6][SOUND_SHOT_HIT] = 25;
232 233 234
    sndSlotMapGalaxy[6][SOUND_CEILICK_LICK] = 26;
    sndSlotMapGalaxy[6][SOUND_BABOBBA_JUMP] = 27;
    sndSlotMapGalaxy[6][SOUND_BABOBBA_LAND] = 28;
235
    sndSlotMapGalaxy[6][SOUND_AUTOGUN] = 29;
236
    //sndSlotMapGalaxy[6][?] = 30;
237
    sndSlotMapGalaxy[6][SOUND_BOBBA_LAND] = 31;
Gerhard Stein's avatar
Gerhard Stein committed
238
    sndSlotMapGalaxy[6][SOUND_BOBBA_SHOOT] = 32;
239
    sndSlotMapGalaxy[6][SOUND_SATELITE_MOUNT] = 33;
240 241
    sndSlotMapGalaxy[6][SOUND_STATUS_SLIDE_IN] = 34;
    sndSlotMapGalaxy[6][SOUND_STATUS_SLIDE_OUT] = 35;
242 243 244 245 246
    //sndSlotMapGalaxy[6][SOUND_GIK_JUMP] = 36;
    //sndSlotMapGalaxy[6][SOUND_GIK_LAND] = 37;
    sndSlotMapGalaxy[6][SOUND_ORBATRIX_BUMP] = 38;
    //sndSlotMapGalaxy[6][SOUND_LASER_SHOT] = 39;
    //sndSlotMapGalaxy[6][SOUND_BIG_LASER_SHOT] = 40;
247
    sndSlotMapGalaxy[6][SOUND_TELEPORT] = 41;
248
    //sndSlotMapGalaxy[6][SOUND_SPRING] = 42;
249
    sndSlotMapGalaxy[6][SOUND_FLAG_APPEAR] = 43;
gerstrong's avatar
gerstrong committed
250
    sndSlotMapGalaxy[6][SOUND_FLAG_LAND] = 44;
251
    sndSlotMapGalaxy[6][SOUND_GET_SPECIAL_ITEM] = 45;
252
    //sndSlotMapGalaxy[6][?] = 46;
253 254 255 256 257 258 259 260 261 262 263 264
    sndSlotMapGalaxy[6][SOUND_PLAYER_PADDLE] = 46;
    sndSlotMapGalaxy[6][SOUND_COMPUTER_PADDLE] = 47;
    sndSlotMapGalaxy[6][SOUND_HIT_SIDEWALL] = 48;
    sndSlotMapGalaxy[6][SOUND_COMPUTER_POINT] = 49;
    sndSlotMapGalaxy[6][SOUND_PLAYER_POINT] = 50;
    sndSlotMapGalaxy[6][SOUND_GRABBITER_SLEEP] = 51;
    sndSlotMapGalaxy[6][SOUND_KEEN_STUNNED] = 52;
    sndSlotMapGalaxy[6][SOUND_ROPE_THROW] = 53;    
    sndSlotMapGalaxy[6][SOUND_ROCKET_DRIVE] = 54;
    sndSlotMapGalaxy[6][SOUND_CEILICK_LAUGH] = 55;
    sndSlotMapGalaxy[6][SOUND_ROCKET_LAUNCH] = 56;
    sndSlotMapGalaxy[6][SOUND_GRABBITER_HUNGRY] = 57;
265
    sndSlotMapGalaxy[6][SOUND_BLOOGGUARD_STUB] = 58;
266 267
    sndSlotMapGalaxy[6][SOUND_BABOBBA_CINDER] = 59;
    
gerstrong's avatar
gerstrong committed
268

269 270 271
}


272
/**
Gerhard Stein's avatar
Gerhard Stein committed
273 274
 * \brief 	This function will load the sounds using other dictionaries which are embedded in the Exe File.
 * 			Only galaxy supports that feature, and the original games will read two files from the EXE-file
275 276 277 278 279
 * 			AUDIOHED and AUDIODICT to get the sounds.
 * 			Caution: CMusic Class has a function which is similar but only loads the music from one level.
 */
bool CAudioGalaxy::LoadFromAudioCK(const CExeFile& ExeFile)
{
280
    setupAudioMap();
gerstrong's avatar
gerstrong committed
281

282 283 284 285
	if(m_AudioSpec.format != 0)
	{
		// Open the Huffman dictionary and get AUDIODICT
		CHuffman Huffman;
gerstrong's avatar
gerstrong committed
286

Gerhard Stein's avatar
Gerhard Stein committed
287
		std::string audioDictfilename = getResourceFilename( gpResource->audioDictFilename, ExeFile.getDataDirectory(), false, false);
gerstrong's avatar
gerstrong committed
288

Gerhard Stein's avatar
Gerhard Stein committed
289 290 291 292
		if(audioDictfilename.empty())
		    Huffman.readDictionaryNumber( ExeFile, 0 );
		else
		    Huffman.readDictionaryFromFile( audioDictfilename );
293 294 295

		/// First get the size of the AUDIO.CK? File.
		uint32_t audiofilecompsize;
Gerhard Stein's avatar
Gerhard Stein committed
296
		std::string init_audiofilename = gpResource->audioFilename;
297 298 299 300

		std::string audiofilename = getResourceFilename( init_audiofilename, ExeFile.getDataDirectory(), true, false);

		if( audiofilename == "" )
Gerstrong's avatar
Gerstrong committed
301
		{
302
			gLogging.textOut("CAudioGalaxy::LoadFromAudioCK(): Audio File not found!");
303
			return false;
gerstrong's avatar
gerstrong committed
304
		}
305 306 307 308 309 310 311 312 313 314 315 316 317 318

		std::ifstream AudioFile;
		OpenGameFileR(AudioFile, audiofilename);

		// Read File Size and allocate memory so we can read it
		AudioFile.seekg( 0, std::ios::end );
		audiofilecompsize = AudioFile.tellg();
		AudioFile.seekg( 0, std::ios::beg );

		// create memory so we can store the Audio.ck there and use it later for extraction
		uint8_t *AudioCompFileData = new uint8_t[audiofilecompsize];
		AudioFile.read((char*)AudioCompFileData, audiofilecompsize);
		AudioFile.close();

Gerhard Stein's avatar
Gerhard Stein committed
319
		// Open the AUDIOHED so we know where to decompress the audio
gerstrong's avatar
gerstrong committed
320

Gerhard Stein's avatar
Gerhard Stein committed
321
		std::string audiohedfilename = gpResource->audioHedFilename;
Gerhard Stein's avatar
Gerhard Stein committed
322
		audiohedfilename = getResourceFilename( audiohedfilename, ExeFile.getDataDirectory(), false, false);
gerstrong's avatar
gerstrong committed
323

Gerhard Stein's avatar
Gerhard Stein committed
324 325
		uint32_t *audiostarthedptr;
		uint32_t *audioendhedptr;
gerstrong's avatar
gerstrong committed
326

Gerhard Stein's avatar
Gerhard Stein committed
327
		std::vector<uint32_t> audiohed;
gerstrong's avatar
gerstrong committed
328 329

		if(audiohedfilename != "")
330
		{
gerstrong's avatar
gerstrong committed
331
		    std::ifstream File;
Gerhard Stein's avatar
Gerhard Stein committed
332
		    OpenGameFileR(File, audiohedfilename, std::ios::binary);
gerstrong's avatar
gerstrong committed
333

Gerhard Stein's avatar
Gerhard Stein committed
334 335 336
		    File.seekg (0, std::ios::end);
		    size_t length = File.tellg();
		    File.seekg (0, std::ios::beg);
gerstrong's avatar
gerstrong committed
337

Gerhard Stein's avatar
Gerhard Stein committed
338
		    audiohed.resize(length/sizeof(u_int));
gerstrong's avatar
gerstrong committed
339 340

		    File.read( (char*) &(audiohed.front()), length);
Gerhard Stein's avatar
Gerhard Stein committed
341 342 343 344 345
		}
		else // no file found? Use the embedded one!
		{
		    audiostarthedptr = reinterpret_cast<uint32_t*>(ExeFile.getHeaderData());
		    audioendhedptr = audiostarthedptr + ExeFile.getExeDataSize()/sizeof(uint32_t);
gerstrong's avatar
gerstrong committed
346

Gerhard Stein's avatar
Gerhard Stein committed
347 348 349 350
		    uint32_t *audiohedptr = audiostarthedptr;
		    uint32_t number_of_audiorecs = 0;
		    for( ; audiohedptr < audioendhedptr ; audiohedptr++ )
		    {
351 352
			if(*audiohedptr == audiofilecompsize)
			{
353
				for( const uint32_t *startptr = (uint32_t*) (void*) ExeFile.getHeaderData() ;
354 355 356 357 358 359 360 361 362
						audiohedptr > startptr ; audiohedptr-- )
				{
					// Get the number of Audio files we have
					number_of_audiorecs++;
					if(*audiohedptr == 0x0)
						break;
				}
				break;
			}
Gerhard Stein's avatar
Gerhard Stein committed
363
		    }
gerstrong's avatar
gerstrong committed
364

Gerhard Stein's avatar
Gerhard Stein committed
365 366 367 368 369
		    for(size_t i=0 ; i<number_of_audiorecs ; i++)
		    {
			audiohed.push_back(*audiohedptr);
			audiohedptr++;
		    }
gerstrong's avatar
gerstrong committed
370
		}
371

Gerhard Stein's avatar
Gerhard Stein committed
372 373

		if(audiohed.empty())
Gerstrong's avatar
Gerstrong committed
374
		{
375
			gLogging.textOut("CAudioGalaxy::LoadFromAudioCK(): No audio was found in that file! It seems to be empty.");
Gerstrong's avatar
Gerstrong committed
376
			delete [] AudioCompFileData;
377
			return false;
Gerstrong's avatar
Gerstrong committed
378
		}
379 380 381 382 383


		// Find the start of the embedded IMF files
		uint32_t outsize = 0;
		uint32_t al_snd_start = 0;
384
		uint32_t number_of_total_sounds = 0;
385

Gerhard Stein's avatar
Gerhard Stein committed
386
		for(  ; number_of_total_sounds<audiohed.size() ; number_of_total_sounds++ )
387
		{
Gerhard Stein's avatar
Gerhard Stein committed
388 389
			const uint32_t audio_start = audiohed[number_of_total_sounds];
			const uint32_t audio_end = audiohed[number_of_total_sounds+1];
390 391 392

			if(audio_start == audio_end)
			{
393
				al_snd_start = number_of_total_sounds/2;
394 395 396 397
				break;
			}
		}

Gerhard Stein's avatar
Gerhard Stein committed
398
		m_soundslot.assign(number_of_total_sounds, CSoundSlot());
gerstrong's avatar
gerstrong committed
399

Gerstrong's avatar
Gerstrong committed
400
		for( unsigned int snd=0 ; snd<number_of_total_sounds ; snd++ )
401 402 403
		{
			/// Now we have all the data we need.
			// decompress every file of AUDIO.CK? using huffman decompression algorithm
Gerhard Stein's avatar
Gerhard Stein committed
404 405
			const uint32_t audio_start = audiohed[snd];
			const uint32_t audio_end = audiohed[snd+1];
406

407
			const uint32_t audio_comp_data_start = audio_start+sizeof(uint32_t); // Why this strange offset by 4 bytes?
Gerhard Stein's avatar
Gerhard Stein committed
408
			if( audio_comp_data_start < audio_end )
gerstrong's avatar
gerstrong committed
409
			{
Gerhard Stein's avatar
Gerhard Stein committed
410
				const uint32_t *AudioCompFileData32 = reinterpret_cast<uint32_t*>(
Gerhard Stein's avatar
Gerhard Stein committed
411
								reinterpret_cast<void*>(AudioCompFileData + audio_start));
Gerhard Stein's avatar
Gerhard Stein committed
412

413
				outsize = *AudioCompFileData32;
Gerstrong's avatar
Gerstrong committed
414
				byte imfdata[outsize];
415 416 417

				Huffman.expand( (byte*)(AudioCompFileData+audio_comp_data_start), imfdata, audio_end-audio_comp_data_start, outsize);

418
				if(snd>=al_snd_start)
Gerhard Stein's avatar
Gerhard Stein committed
419
					readISFintoWaveForm( m_soundslot[snd], imfdata, outsize, (m_AudioSpec.format == AUDIO_S16) ? 2 : 1 );
420
				else
Gerhard Stein's avatar
Gerhard Stein committed
421
					readPCSpeakerSoundintoWaveForm( m_soundslot[snd], imfdata, outsize, (m_AudioSpec.format == AUDIO_S16) ? 2 : 1 );
422 423
			}
		}
Gerstrong's avatar
Gerstrong committed
424 425

		delete [] AudioCompFileData;
426
	}
Gerstrong's avatar
Gerstrong committed
427
	else
428
		gLogging.textOut("CAudioGalaxy::LoadFromAudioCK(): Wrong Audio Format");
Gerstrong's avatar
Gerstrong committed
429 430

	return true;
431 432
}

433 434 435 436 437
/**
 * Main load function for the galaxy audio
 */
bool CAudioGalaxy::loadSoundData()
{
438
    COPLEmulator &OPLEmulator = *g_pSound->getOPLEmulatorPtr();
gerstrong's avatar
gerstrong committed
439

440 441
    OPLEmulator.shutdown();
    OPLEmulator.init();
gerstrong's avatar
gerstrong committed
442

443
	const bool ok = LoadFromAudioCK(m_ExeFile);
444

445
	if(!ok)
446
	{
447
		gLogging.textOut("CAudioGalaxy::loadSoundData(): The function cannot read audio of that game");
448 449 450
		return false;
	}

451
	return true;
452 453 454 455 456 457 458 459 460
}

/**
 * Will free the resources of the audio
 */
void CAudioGalaxy::unloadSound()
{

}