Okay, this is the code so far (I basically copied and pasted it from another thread on this forum). It plays a tone.
#include "stdafx.h"
#include <dsound.h>
#define _USE_MATH_DEFINES
#define _WIN32_DCOM
#include <cmath>
#include <string>
#include <iostream>
#include <time.h>
using namespace std;
LPDIRECTSOUND8 lpds;
LPDIRECTSOUNDBUFFER lpdsbuffer;
WAVEFORMATEX wfx;
DSBUFFERDESC dsbdesc;
HWND hwnd;
short* buffer = new short[ 44100 * 2 ];
bool playing = false;
int SampleRate = 44100;
//*************************
//
//*************************
HWND GetConsoleHwnd( void )
{
SetConsoleTitle( ( LPCWSTR ) L"Title" );
hwnd = FindWindow( NULL, ( LPCWSTR ) L"Title" );
return(hwnd);
}
//*************************
//
//*************************
HRESULT createSoundObject( void )
{
HRESULT hr;
hr = DirectSoundCreate8( NULL , &lpds , NULL );
hr = CoInitializeEx( NULL , 0 );
hr = lpds->SetCooperativeLevel( hwnd , DSSCL_NORMAL );
return hr;
}
//*************************
//
//*************************
WAVEFORMATEX setWaveFormat( void )
{
WAVEFORMATEX wfx;
memset( &wfx, 0, sizeof( WAVEFORMATEX ) );
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = 2;
wfx.wBitsPerSample = 16;
wfx.nSamplesPerSec = SampleRate; //44100
wfx.nBlockAlign = 4;
wfx.nAvgBytesPerSec = SampleRate * 4;
return wfx;
}
//*************************
//
//*************************
DSBUFFERDESC setBufferDescription()
{
DSBUFFERDESC dsbdesc;
memset( &dsbdesc, 0, sizeof ( DSBUFFERDESC ) );
dsbdesc.dwSize = sizeof ( DSBUFFERDESC );
dsbdesc.dwFlags = DSBCAPS_CTRLPOSITIONNOTIFY | DSBCAPS_GETCURRENTPOSITION2;
dsbdesc.dwBufferBytes = 1024;
dsbdesc.lpwfxFormat = &wfx;//<- Set wave format
return dsbdesc;
}
//*************************
//
//*************************
HRESULT createSecondarySoundBuffer( void )
{
HRESULT hr = lpds->CreateSoundBuffer( &dsbdesc , &lpdsbuffer , NULL );
return hr;
}
//*************************
//
//*************************
HRESULT fillSoundBuffer( bool first )
{
bool firsthalf = first;
LPVOID lpvWrite;
DWORD dwLength;
HRESULT hr = lpdsbuffer->Lock( 0 , dsbdesc.dwBufferBytes , &lpvWrite , &dwLength , NULL , NULL , DSBLOCK_ENTIREBUFFER );
if ( SUCCEEDED ( hr ) )
{
memcpy( lpvWrite , buffer , dwLength );
hr = lpdsbuffer->Unlock( lpvWrite , dwLength , NULL , NULL );
}
return hr;
}
//*************************
//
//*************************
BOOL fillBufferWithWaveForm( string form ){
if( form == "sine" )
{
short* ptr = buffer;
unsigned short i;
float amp = 1.0f;
float tmp;
short output;
for( i=0 ; i < 44100 ; i++ )
{
tmp = amp * sinf(100.0f * 2.0f * M_PI * ( ( float ) i / 44100.0f ) );
output = ( short )( tmp*32767 );
ptr[0] = output;
ptr[1] = output;
ptr += 2;
}
return true;
}
}
//*************************
//
//*************************
void SetupSound()
{
HRESULT hr;
BOOL formFound = false;
string waveform = "sine";
// Rename the ConsoleWindow and retrieve its
// handle by looking up its new name in Windows
hwnd = GetConsoleHwnd();
// Create the Basic LPDIRECTSOUND8 object
hr = createSoundObject();
// Set the default wave format (hard coded)
wfx = setWaveFormat();
// Set the default buffer description (hard coded)
dsbdesc = setBufferDescription();
// Create the secondary sound buffer
hr = createSecondarySoundBuffer();
// Generate a waveform and put it into the playback array
formFound = fillBufferWithWaveForm(waveform);
// fill the secondary sound buffer entirely with the playback array data
hr = fillSoundBuffer( true );
// Start playback at position 0 and loop this sound until explicitely stopped
lpdsbuffer->SetCurrentPosition( 0 );
//DSBPLAY_DEFAULT
//DSBPLAY_LOOPING
}
//*************************
//
//*************************
int _tmain( int argc , char* argv[] )
{
SetupSound();
lpdsbuffer->Play( 0 , 0 , DSBPLAY_LOOPING );
lpdsbuffer->SetFrequency(1300);//<---- not working!!!
system("PAUSE");
lpdsbuffer->Release();
lpds->Release();
return 0;
}
However I can't seem to get the frequency to change by calling lpdsbuffer->SetFrequency(). Is this some bug with DirectSound?