TGC Codebase Backup



[DGDK]Memblock Sound Generator by Mista Wilson

15th Aug 2009 3:09
Summary

Memblock Sound Generator uses memblocks to create medialess sound files, great for simple buzzes, beeps and whistles...



Description

This is a piece of code ported from a DBPro snippet to generate sounds using a single function that creates a memblock and the sound, based on the parameters you pass the function when you call it.

Code required that sound number 999 be not in use(you can change this in the code if you like)



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    ///////////////////////////////////////////////////
//DBPro Sound Creator by Ric, and:
// 
//Mista Wilson - DarkGDK Port(code below)
//Tommy S - Original waveform code
//Green Gandalf - Memblock format
//Lampton Worm - Load/Save format
//Lampton Worm/Raven - Real time parameter editor
//Xtom - GUI/parameter editor
/////////////////////////////////////////////////////
#include "DarkGDK.h"

void createsound(char* name, int soundnumber, float frequency, float length, float loudness, float bend, 
				 float decay, float vibratospeed, float vibratodepth, float tremelospeed, float tremelodepth, float attack);

// the main entry point for the application is this function
void DarkGDK ( void )
{

	dbSyncOn();
	dbSyncRate(60);
	dbAutoCamOff();
	dbBackdropOn(0);
	dbColorBackdrop(0, dbRGB(60, 40, 120));

	createsound("sound effect",1,2995,2000,8000,0.02,0.1,0.60,3,0.64,5,10);
	createsound("explosion",2,6200,200,2000,20,0.2,0,0,0,0,10);
	createsound("explosion2",3,1200,600,3000,10,0.1,0,0,0,0,10);
	createsound("powerup",4,800,700,8000,-0.02,0.2,0,0,0,0,10);
	createsound("warning",5,800,1000,6000,0.5,0.1,0,0,0,0,5);
	createsound("vibrato1",6,500,2000,8000,0,0.1,0.05,0.2,0,0,1);
	createsound("vibrato2",7,400,2000,8000,0,0.1,0.05,0.2,0,0,1);
	createsound("vibrato3",8,600,2000,8000,0,0.1,0.05,0.2,0,0,1);
	createsound("bass hit",9,30,50,16000,0.00,1.5,.0,0,0,0,100);
	createsound("hihat",10,80,1000,5000,70.06,2.5,.1,0,0,0,2);
	createsound("door knock",11,200,20,8000,.1,4.0,2.1,10,9.2,9.2,10);
	createsound("vinyl scratch",12,500,80,8000,0.06,0.2,.1,0,0,0,10);
	createsound("cb1",13,250,50,2500,0.00001,0.1,4.0,0.4,0,0,10);
	createsound("cb2",14,500,50,2500,0.00001,0.1,4.0,0.4,0,0,10);
	createsound("cb3",15,437.5,150,1500,0.0001,0.1,0.0,4.0,0.4,0,10);
	createsound("cb4",16,573.75,150,1500,0.0,0.1,4.0,0.4,0.1,0.1,10);
	createsound("bubble burst",17,50,80,6000,-.4,2.0,0.1,10,1.2,0.2,10);

	dbSetTextFont("arial");
	dbSetTextSize(16);

	// our main loop
	while ( LoopGDK ( ) )
	{
		
		dbText(0, 0,"Press keys 1 upwards:");
		dbText(0, 20,"1: sound effect");
		dbText(0, 40,"2: explosion1");
		dbText(0, 60,"3: explosion2");
		dbText(0, 80,"4: powerup");
		dbText(0, 100,"5: warning");
		dbText(0, 120,"6: vibrato");
		dbText(0, 140,"7: bass hit");
		dbText(0, 160,"8: hihat");
		dbText(0, 180,"9: door knock");
		dbText(0, 200,"0: vinyl scratch");
		dbText(0, 220,"-: cowbell");
		dbText(0, 240,"+: bubble burst");

		if(dbKeyState(2) == 1) { dbPlaySound(1); dbWait(300); }
		if(dbKeyState(3) == 1) { dbPlaySound(2); dbWait(300); }
		if(dbKeyState(4) == 1) { dbPlaySound(3); dbWait(300); }
		if(dbKeyState(5) == 1) { dbPlaySound(4); dbWait(300); }
		if(dbKeyState(6) == 1) { dbPlaySound(5); dbWait(300); }
		if(dbKeyState(7) == 1) { dbPlaySound(6);dbPlaySound(7);dbPlaySound(8); dbWait(300); }
		if(dbKeyState(8) == 1) { dbPlaySound(9); dbWait(300); }
		if(dbKeyState(9) == 1) { dbPlaySound(10); dbWait(300); }
		if(dbKeyState(10) == 1) { dbPlaySound(11); dbWait(300); }
		if(dbKeyState(11) == 1) { dbPlaySound(12); dbWait(300); }
		if(dbKeyState(12) == 1) { dbPlaySound(13);dbPlaySound(14);dbPlaySound(15);dbPlaySound(16);dbWait(300); }
		if(dbKeyState(13) == 1) { dbPlaySound(17); dbWait(300); }

		// update the screen
		dbSync ( );
	}
	// return back to windows
	return;
}
// Memblock sound creation function - ported from DBPro by Paul A Wilson
void createsound(char* name, int soundnumber, float frequency, float length, float loudness, float bend, float decay, 
				 float vibratospeed, float vibratodepth, float tremelospeed, float tremelodepth, float attack)
{

WORD outWord = 0;
int outInteger = 0;
DWORD dword1;
DWORD dword2;
DWORD dword3;
DWORD dword4;
DWORD dword5;
DWORD dword6;
DWORD dword7;

int samples = ((length / 1000) * 44100);

if(dbMemblockExist(1)) dbDeleteMemblock(1);
dbMakeMemblock(1, samples * 2 + 28);

//write 28 memblock header bytes
dword1 = 1;      // gg query: is this the number of channels?
dword2 = 2;      // gg query: is this the number of bytes per sample?
dword3 = 22050;  // gg query: seems to be half the number of samples per second - why?
dword4 = 88200;  // gg query: is this the number of bytes per second?
dword5 = 4;      // gg query: what does this represent?
dword6 = 16;     // gg query:     (ditto)            ?
dword7 = 0;      // gg query:     (ditto)            ?

int position = 0;
dbWriteMemblockDword(1, position, dword1); position += 4;
dbWriteMemblockDword(1, position, dword2); position += 4;
dbWriteMemblockDword(1, position, dword3); position += 4;
dbWriteMemblockDword(1, position, dword4); position += 4;
dbWriteMemblockDword(1, position, dword5); position += 4;
dbWriteMemblockDword(1, position, dword6); position += 4;
dbWriteMemblockDword(1, position, dword7); position += 4;

//rem generate and write wave
float theta = 0;
float phi = 0;
float fallinloudness = loudness;
float riseinloudness = loudness;
for(int x = 1; x <= samples; ++x)
{
  outInteger = (int)(dbSin(( x / 122.5) * (frequency + vibratodepth * dbSin(theta))) * (loudness - fallinloudness - riseinloudness + tremelodepth * dbSin(phi))) * 3.0;
  if(outInteger < -32767) outInteger = -32767;	// gg query: is this the valid range?
  if(outInteger > 32767) outInteger = 32767;	// gg query:       (ditto)          ?
  outWord = outInteger;
  theta += vibratospeed;
  phi += tremelospeed;
  frequency -= bend;

  if(fallinloudness < loudness)
  {
    fallinloudness += decay;
  }

  if(riseinloudness > 0)
  {
    riseinloudness -= attack;
  }

  dbWriteMemblockWord(1, position, outWord); position += 2;
}

if(dbSoundExist(soundnumber) == 1) dbDeleteSound(soundnumber);

dbMakeSoundFromMemblock(999, 1); // assumes you won't need sound number 999!
dbCloneSound(soundnumber, 999);
dbDeleteSound(999);

//memblock no longer required
dbDeleteMemblock(1);

};