topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Sunday December 15, 2024, 1:53 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Playing with TDx and sound buffers  (Read 6358 times)

Shed

  • Honorary Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 26
    • View Profile
    • Donate to Member
Playing with TDx and sound buffers
« on: May 03, 2006, 12:39 PM »

Another app for contest...showing how code and play some audio buffers with c++ builder, TDx Library and LMD Tools....

- source code project included...
- not finnished application...see better like code tutorial demo.


Shed

  • Honorary Member
  • Joined in 2006
  • **
  • default avatar
  • Posts: 26
    • View Profile
    • Donate to Member
Re: Playing with TDx and sound buffers
« Reply #1 on: May 03, 2006, 07:27 PM »
and here the source code for main file

//---------------------------------------------------------------------------
// Author: Shed of BCB-Tools Pty. Ltd  round year 2005-2006
// Compiler: Made under Borland C++ Builder 6 Ent.
// Requeriments for rebuild:  LMD Tools SE 6.1 and TDx Libraries v1.70
// Requeriments for sound editting: Goldwave editor in any version...better recent.
// Copyright: use this code at your own convenience, greetings to BCB-Tools is
//            enough...enjoy with it... :)
//
// Not expect so much as you can see there in main screen...this program it's
// only a demonstration how to play multiple sounds with TDx Libs only...
// if you want modify for make your own version of this "jukebox" of sounds
// feel freely to do it...it's intended for this stuff...
// You can see that in sounds folder, you have more than in mainform...yes
// this is so because is for future editting and implementation...

// Thanks to people like Mouser and Donationcoder crew, Darren, ElPusher,
// Dosfreak, Paskuator, Drull, A78 etc etc
// for be there, and support me and my bad days ...thanks a lot guys ;)




#include <vcl.h>
#pragma hdrstop
#pragma link "TDx_Sound"
#pragma link "TDx_SoundBuffer"
#pragma link "TDSCaps"
#pragma link "TDSBufferDesc"
#pragma link "TDx_Sound_Library_Install"

#include "Unit1.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "LMDBaseEdit"
#pragma link "LMDButton"
#pragma link "LMDCustomBevelPanel"
#pragma link "LMDCustomButton"
#pragma link "LMDCustomComponent"
#pragma link "LMDCustomControl"
#pragma link "LMDCustomEdit"
#pragma link "LMDCustomExtSpinEdit"
#pragma link "LMDCustomFormFill"
#pragma link "LMDCustomMaskEdit"
#pragma link "LMDCustomPanel"
#pragma link "LMDFormFill"
#pragma link "LMDWndProcComponent"
#pragma link "TDSBufferDesc"
#pragma link "TDx_Sound"
#pragma link "TDx_SoundBuffer"
#pragma link "LMDCustomPanelFill"
#pragma link "LMDCustomParentPanel"
#pragma link "LMDPanelFill"
#pragma link "LMDSBar"
#pragma link "LMDBaseControl"
#pragma link "LMDBaseGraphicButton"
#pragma link "LMDControl"
#pragma link "LMDCustomShapeButton"
#pragma link "LMDShapeButton"
#pragma link "LMDBaseGraphicControl"
#pragma link "LMDClock"
#pragma link "LMDGraphicControl"
#pragma link "LMDBaseMeter"
#pragma link "LMDCustomProgressFill"
#pragma link "LMDProgressFill"
#pragma link "lmdsbar"
#pragma link "LMDButton"
#pragma link "LMDCustomBevelPanel"
#pragma link "LMDCustomButton"
#pragma link "LMDCustomControl"
#pragma link "LMDCustomPanel"
#pragma resource "*.dfm"

 //---------------------------------------------------------------------------
// DEBUGGING
//---------------------------------------------------------------------------
#define Msg(A)  {\
                if (StatusMemo->Text.Length()>30000) StatusMemo->Text = "";\
                StatusMemo->Lines->Add(A);\
                }

void volume_ramp(void);
TForm1 *Form1;

int tiempo_fade_off = 0;
long time_fade = 0;
float segundos_del_efecto = 0;
float counter_temp1 = 0;

HMUSIC cancion_actual;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
 Form1->VolumeScrollBar->DoubleBuffered = True;
 Form1->DoubleBuffered = True;

///////////////////////////////////////This part only for BASS audio library /////////

  ShowMessage(BASS_GetVersion());
  if (BASS_GetVersion() != MAKELONG(2,0))
  throw Exception("No se pudo cargar la versión 2 de BASS");

  if (!BASS_Init(1, 44100, 0, Handle, 0))
  throw Exception("Error inicializando Bass Library");

///////////////////////////////////////////////////////////////////////////////////////

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Dx_Sound1Create(TObject *Sender)
{
//Change the cooperative level and load the sounds into the buffers
Dx_Sound1->SetCooperativeLevel( Application->Handle, DSSCL_PRIORITY );
Dx_SoundBuffer1->CreateFromFile( ".\\snd\\rain_loop.wav", DSBufferDesc1, Dx_Sound1 );
Dx_SoundBuffer2->CreateFromFile( ".\\snd\\thunder.wav", DSBufferDesc2, Dx_Sound1 );
Dx_SoundBuffer3->CreateFromFile( ".\\snd\\medrain.wav", DSBufferDesc3, Dx_Sound1 );
Dx_SoundBuffer4->CreateFromFile( ".\\snd\\ocean_waves.wav", DSBufferDesc4, Dx_Sound1 );
Dx_SoundBuffer5->CreateFromFile( ".\\snd\\water_river.wav", DSBufferDesc5, Dx_Sound1 );
Dx_SoundBuffer6->CreateFromFile( ".\\snd\\birds1.wav", DSBufferDesc6, Dx_Sound1 );
Dx_SoundBuffer7->CreateFromFile( ".\\snd\\crickets.wav", DSBufferDesc7, Dx_Sound1 );
Dx_SoundBuffer8->CreateFromFile( ".\\snd\\port1.wav", DSBufferDesc8, Dx_Sound1 );

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Dx_Sound1Destroy(TObject *Sender)
{
 if (Dx_SoundBuffer1->Created) Dx_SoundBuffer1->Destroy();
 if (Dx_SoundBuffer2->Created) Dx_SoundBuffer2->Destroy();
 if (Dx_SoundBuffer3->Created) Dx_SoundBuffer3->Destroy();
 if (Dx_SoundBuffer4->Created) Dx_SoundBuffer4->Destroy();
 if (Dx_SoundBuffer5->Created) Dx_SoundBuffer5->Destroy();
 if (Dx_SoundBuffer6->Created) Dx_SoundBuffer6->Destroy();
 if (Dx_SoundBuffer7->Created) Dx_SoundBuffer7->Destroy();
 if (Dx_SoundBuffer8->Created) Dx_SoundBuffer8->Destroy();
 if (Dx_SoundBuffer9->Created) Dx_SoundBuffer9->Destroy();
 if (Dx_SoundBuffer10->Created) Dx_SoundBuffer10->Destroy();

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Dx_Sound1DSEnumerate(TObject *Sender, GUID *Guid,
      AnsiString DriverDescription, AnsiString DriverModule, void *Context,
      bool &Finished)
{
Msg( "Found Device: "+DriverDescription );
if (KnownDevices && KnownModules && KnownGuids)
    {
    KnownDevices->Add( DriverDescription );
    KnownModules->Add( DriverModule );
    KnownGuids->Add( (void*) Guid );

    DevicesListBox->Items->Add( DriverDescription+", ("+DriverModule+")" );
    }
else
    Finished = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 KnownDevices = new TStringList();
 KnownModules = new TStringList();
 KnownGuids = new TList();

 Msg( "Enumerating DirectSound devices..." );

KnownGuids->Clear();
KnownDevices->Clear();
KnownModules->Clear();
DevicesListBox->Items->Clear();

if (!Dx_Sound1->DSEnumerate( NULL )) Msg("No available DirectSound devices!");

ActiveDeviceLabel->Caption = "none";
TotalDevicesLabel->Caption = IntToStr(KnownDevices->Count);

Msg( "Found: "+IntToStr(KnownDevices->Count)+" DirectSound Devices" );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
  if (Dx_SoundBuffer1->Created)
     Dx_SoundBuffer1->Destroy();
 if (Dx_SoundBuffer2->Created)
     Dx_SoundBuffer2->Destroy();
 if (Dx_SoundBuffer3->Created)
     Dx_SoundBuffer3->Destroy();
 if (Dx_SoundBuffer4->Created)
     Dx_SoundBuffer4->Destroy();
 if (Dx_SoundBuffer5->Created)
     Dx_SoundBuffer5->Destroy();


 if (Dx_Sound1->Created) Dx_Sound1->Destroy();

 if (KnownGuids) delete KnownGuids;
 if (KnownModules) delete KnownModules;
 if (KnownDevices) delete KnownDevices;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DevicesListBoxClick(TObject *Sender)
{

  if (Dx_SoundBuffer1->Created)
     Dx_SoundBuffer1->Destroy();
 if (Dx_SoundBuffer2->Created)
     Dx_SoundBuffer2->Destroy();
 if (Dx_SoundBuffer3->Created)
     Dx_SoundBuffer3->Destroy();
 if (Dx_SoundBuffer4->Created)
     Dx_SoundBuffer4->Destroy();
 if (Dx_SoundBuffer5->Created)
     Dx_SoundBuffer5->Destroy();
 if (Dx_SoundBuffer6->Created)
     Dx_SoundBuffer6->Destroy();
 if (Dx_SoundBuffer7->Created)
     Dx_SoundBuffer7->Destroy();
 if (Dx_SoundBuffer8->Created)
     Dx_SoundBuffer8->Destroy();
 if (Dx_SoundBuffer9->Created)
     Dx_SoundBuffer9->Destroy();
 if (Dx_SoundBuffer10->Created)
     Dx_SoundBuffer10->Destroy();

 if (Dx_Sound1->Created) Dx_Sound1->Destroy();

NotCreatedLabel->Visible = !Dx_Sound1->Create( (GUID*) KnownGuids->Items[DevicesListBox->ItemIndex] );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox_lluviaClick(TObject *Sender)
{

  //Sound of hard rain, with floor stoned
 if(CheckBox_lluvia->Checked == True)
 {
   Dx_SoundBuffer1->SetVolume(0);
   Dx_SoundBuffer1->Play(0,DSBPLAY_LOOPING);
  }

 else {
        Dx_SoundBuffer1->Stop();
        }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox_med_rainClick(TObject *Sender)
{

 //Sound of light rain
 if(CheckBox_med_rain->Checked == True)
 {
   Dx_SoundBuffer3->SetVolume(0);
   Dx_SoundBuffer3->Play(0,DSBPLAY_LOOPING);
  }

 else {
        Dx_SoundBuffer3->Stop();
        }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox_oceanClick(TObject *Sender)
{
  //Relaxing with sound of waves breaking in the coast
 if(CheckBox_ocean->Checked == True)
 {
   Dx_SoundBuffer4->SetVolume(0);
   Dx_SoundBuffer4->Play(0,DSBPLAY_LOOPING);
  }

 else {
        Dx_SoundBuffer4->Stop();
        }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::CheckBox_riverClick(TObject *Sender)
{
  // Small river, water sound
  if(CheckBox_river->Checked == True)
 {
   Dx_SoundBuffer5->SetVolume(0);
   Dx_SoundBuffer5->Play(0,DSBPLAY_LOOPING);
  }

 else {
        Dx_SoundBuffer5->Stop();
        }
}

//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox_cricketsClick(TObject *Sender)
{
  // Nice sound of Crickets at the grass
   if(CheckBox_crickets->Checked == True)
 {
   Dx_SoundBuffer7->SetVolume(0);
   Dx_SoundBuffer7->Play(0,DSBPLAY_LOOPING);
  }

 else {
        Dx_SoundBuffer7->Stop();
        }
}
//---------------------------------------------------------------------------




void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
  volume_ramp();
}
//---------------------------------------------------------------------------

void volume_ramp(void)
{
  int NOTT = -1;
  //This is the function of Volume Ramp to Down (silence)
  // tiempo_fade_off it's a counter for calculate time of volume decay
  // time_fade it's the interval of decreasing amount of volume
  // The reason to put in a TIMER component...is because this way...
  // meanwhile the Soundbuffer1 volume is down...you can continue working with
  // other controls of application...otherwise...for example if you put into a
  // function with a FOR loop the execution of program it's catched into the
  // loop...and you can't touch anything while the loop isn't finished.

  if(Form1->Dx_SoundBuffer1->Playing == True)
       {
     Form1->Dx_SoundBuffer1->SetVolume( time_fade );
     }
// Buffer nº2 it's reserved for one shot Fx
//  else if (current_buffer_playing[2] == True)
//   {
//     Dx_SoundBuffer2->SetVolume( time_fade );
//     }
  else if (Form1->Dx_SoundBuffer3->Playing == True)
   {
     Form1->Dx_SoundBuffer3->SetVolume( time_fade );
     }
  else if (Form1->Dx_SoundBuffer4->Playing == True)
   {
     Form1->Dx_SoundBuffer4->SetVolume( time_fade );
     }

  else if (Form1->Dx_SoundBuffer5->Playing == True)
   {
     Form1->Dx_SoundBuffer5->SetVolume( time_fade );
     }

  else if (Form1->Dx_SoundBuffer6->Playing == True)
   {
     Form1->Dx_SoundBuffer6->SetVolume( time_fade );
     }

  else if (Form1->Dx_SoundBuffer7->Playing == True)
   {
     Form1->Dx_SoundBuffer7->SetVolume( time_fade );
     }

  else if (Form1->Dx_SoundBuffer8->Playing == True)
   {
     Form1->Dx_SoundBuffer8->SetVolume( time_fade );
     }
  else if (Form1->Dx_SoundBuffer9->Playing == True)
   {
     Form1->Dx_SoundBuffer9->SetVolume( time_fade );
     }
  else if (Form1->Dx_SoundBuffer10->Playing == True)
   {
     Form1->Dx_SoundBuffer10->SetVolume( time_fade );
     }


  //Change the Volume bar, obtaining the range, multiply by -1 and adding to 0.
  //Because directX volume range it's from 0 to -10000 and the volume bar
  //it's numerated from 0 to 10000 (it allow us mantain the orientation of volumebar)
  //this is, MAX volume range on TOP of volume Bar.
  Form1->VolumeScrollBar->Position = 0 + (time_fade * NOTT);
  //counting
  tiempo_fade_off++;
  //and decreasing the volume slide
  time_fade-=90;

  //when tiempo_fade_off reach 60 steps...stop the buffersound
  if (tiempo_fade_off == 70)
   {
    Form1->Timer1->Enabled = False;
    Form1->Dx_SoundBuffer1->Stop();
    Form1->Dx_SoundBuffer2->Stop();
    Form1->Dx_SoundBuffer3->Stop();
    Form1->Dx_SoundBuffer4->Stop();
    Form1->Dx_SoundBuffer5->Stop();
    Form1->Dx_SoundBuffer6->Stop();
    Form1->Dx_SoundBuffer7->Stop();
    Form1->Dx_SoundBuffer8->Stop();
    Form1->Dx_SoundBuffer9->Stop();
    Form1->Dx_SoundBuffer10->Stop();

    //Restore the variables for a new volume ramp down
    tiempo_fade_off = 0;
    time_fade = 0;
    Form1->Timer1->Enabled = False;
     }
 }
void __fastcall TForm1::LMDButton5Click(TObject *Sender)
{
 Form1->Timer1->Enabled = True;
}

//---------------------------------------------------------------------------



void __fastcall TForm1::LMDShapeButton1Click(TObject *Sender)
{
 Form1->Close();
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Timer2Timer(TObject *Sender)
{
  //This timer is used for control the time
  //for dissolve to silende fading out the
  //volume of Fx

 //Fade off the effects to silence
 Timer1->Enabled = True;

   // Reset the timer
 Timer2->Enabled = False;
 Timer2->Interval = 0;

}
//---------------------------------------------------------------------------

void __fastcall TForm1::LMDShapeButton2Click(TObject *Sender)
{
  //Destroy the buffer if created...and load the new sound (and play it :))
  if (Dx_SoundBuffer2->Created)
     Dx_SoundBuffer2->Destroy();
 Dx_SoundBuffer2->CreateFromFile( ".\\snd\\thunder.wav", DSBufferDesc2, Dx_Sound1 );
 Dx_SoundBuffer2->Play(0,0);
}
//---------------------------------------------------------------------------


void __fastcall TForm1::LMDShapeButton4Click(TObject *Sender)
{
  //Destroy the buffer if created...and load the new sound (and play it :))
   if (Dx_SoundBuffer2->Created)
     Dx_SoundBuffer2->Destroy();
 Dx_SoundBuffer2->CreateFromFile( ".\\snd\\birds1.wav", DSBufferDesc2, Dx_Sound1 );
 Dx_SoundBuffer2->Play(0,0);
}
//---------------------------------------------------------------------------


void __fastcall TForm1::LMDShapeButton3Click(TObject *Sender)
{
  //Destroy the buffer if created...and load the new sound (and play it :))
   if (Dx_SoundBuffer2->Created)
     Dx_SoundBuffer2->Destroy();
 Dx_SoundBuffer2->CreateFromFile( ".\\snd\\pass_page.wav", DSBufferDesc2, Dx_Sound1 );
 Dx_SoundBuffer2->Play(0,0);
}
//---------------------------------------------------------------------------



void __fastcall TForm1::LMDShapeButton5Click(TObject *Sender)
{
  //Destroy the buffer if created...and load the new sound (and play it :))
    if (Dx_SoundBuffer2->Created)
     Dx_SoundBuffer2->Destroy();
 Dx_SoundBuffer2->CreateFromFile( ".\\snd\\port1.wav", DSBufferDesc2, Dx_Sound1 );
 Dx_SoundBuffer2->Play(0,0);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::VolumeScrollBarScroll(TObject *Sender,
      TScrollCode ScrollCode, int &ScrollPos)
{
   Volume = 0;
  Volume = Volume - VolumeScrollBar->Position;
 Dx_SoundBuffer1->SetVolume( Volume );
 Dx_SoundBuffer2->SetVolume( Volume );
 Dx_SoundBuffer3->SetVolume( Volume );
 Dx_SoundBuffer4->SetVolume( Volume );
 Dx_SoundBuffer5->SetVolume( Volume );
 Dx_SoundBuffer6->SetVolume( Volume );
 Dx_SoundBuffer7->SetVolume( Volume );
 Dx_SoundBuffer8->SetVolume( Volume );
 Dx_SoundBuffer9->SetVolume( Volume );
 Dx_SoundBuffer10->SetVolume( Volume );
}
//---------------------------------------------------------------------------


void __fastcall TForm1::CheckBox_Birds1Click(TObject *Sender)
{
   // Sound of Birds at the Woods
   if(CheckBox_Birds1->Checked == True)
 {
   Dx_SoundBuffer6->SetVolume(0);
   Dx_SoundBuffer6->Play(0,DSBPLAY_LOOPING);
  }

 else {
        Dx_SoundBuffer6->Stop();
        }
}
//---------------------------------------------------------------------------


void __fastcall TForm1::LMDShapeButton6Click(TObject *Sender)
{
  //Destroy the buffer if created...and load the new sound (and play it :))
    if (Dx_SoundBuffer2->Created)
     Dx_SoundBuffer2->Destroy();
 Dx_SoundBuffer2->CreateFromFile( ".\\snd\\Klick.wav", DSBufferDesc2, Dx_Sound1 );
 Dx_SoundBuffer2->Play(0,0);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::LMDShapeButton7Click(TObject *Sender)
{
   //Destroy the buffer if created...and load the new sound (and play it :))
    if (Dx_SoundBuffer2->Created)
     Dx_SoundBuffer2->Destroy();
 Dx_SoundBuffer2->CreateFromFile( ".\\snd\\shorty_cat.wav", DSBufferDesc2, Dx_Sound1 );
 Dx_SoundBuffer2->Play(0,0);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::LMDShapeButton18Click(TObject *Sender)
{
 ShowMessage("This is an multi-fx ambient simulator, just press buttons...");       
}
//---------------------------------------------------------------------------

void __fastcall TForm1::CheckBox_PuertoClick(TObject *Sender)
{
   //Sound of port ambience with seagulls etc...
 if(CheckBox_Puerto->Checked == True)
 {
   Dx_SoundBuffer8->SetVolume(0);
   Dx_SoundBuffer8->Play(0,DSBPLAY_LOOPING);
  }

 else {
        Dx_SoundBuffer8->Stop();
        }
}
//---------------------------------------------------------------------------
« Last Edit: July 03, 2006, 06:23 AM by Shed »