Anyways I found the following code from another thread of this forum for creating primary and secondary buffers in DirectSound, but it has two linker errors that I'm not sure how to fix...
I have the DirectX SDK installed...
Here are the errors
DirectSound.obj : error LNK2028: unresolved token (0A00028A) "extern "C" long __stdcall DirectSoundCreate8(struct _GUID const *,struct IDirectSound8 * *,struct IUnknown *)" (?DirectSoundCreate8@@$$J212YGJPBU_GUID@@PAPAUIDirectSound8@@PAUIUnknown@@@Z) referenced in function "int __cdecl main(int,char * * const)" (?main@@$$HYAHHQAPAD@Z)
DirectSound.obj : error LNK2019: unresolved external symbol "extern "C" long __stdcall DirectSoundCreate8(struct _GUID const *,struct IDirectSound8 * *,struct IUnknown *)" (?DirectSoundCreate8@@$$J212YGJPBU_GUID@@PAPAUIDirectSound8@@PAUIUnknown@@@Z) referenced in function "int __cdecl main(int,char * * const)" (?main@@$$HYAHHQAPAD@Z)
Code:
//#include "stdafx.h"
#include <dsound.h>
#define _USE_MATH_DEFINES
#define _WIN32_DCOM
#include <cmath>
#include <windows.h>
#include <string>
#include <iostream>
using namespace std;
LPDIRECTSOUND8 lpds;
LPDIRECTSOUNDBUFFER lpdsbuffer;
WAVEFORMATEX wfx;
DSBUFFERDESC dsbdesc;
HWND hwnd;
short* buffer = new short[44100 * 2];
bool playing = false;
HWND GetConsoleHwnd(void){
HWND cWindowHandle;
LPCSTR newtitle = (LPCSTR) L"Audio Programming - Lesson 1";
SetConsoleTitle(newtitle);
Sleep(40);
cWindowHandle=FindWindow(NULL, newtitle);
return(cWindowHandle);
}
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 = 44100;
wfx.nBlockAlign = 4;
wfx.nAvgBytesPerSec = 44100 * 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;
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(1000.0f * 2.0f * M_PI * ((float)i/44100.0f));
output = (short)(tmp*32767);
ptr[0] = output;
ptr[1] = output;
ptr += 2;
}
return true;
}
}
int _tmain(int argc, char* argv[])
{
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);
// Create Notification Handles
HANDLE NotifyEvent[2];
NotifyEvent[0] = CreateEvent(NULL,TRUE,FALSE,NULL);
NotifyEvent[1] = CreateEvent(NULL,TRUE,FALSE,NULL);
LPDIRECTSOUNDNOTIFY8 lpDsNotify;
DSBPOSITIONNOTIFY PositionNotify[2];
if (hr = lpdsbuffer->QueryInterface(IID_IDirectSoundNotify8,(LPVOID*)&lpDsNotify) == DS_OK){
PositionNotify[0].dwOffset = 512;
PositionNotify[0].hEventNotify = NotifyEvent[0];
PositionNotify[1].dwOffset = 1024;
PositionNotify[1].hEventNotify = NotifyEvent[1];
if(hr = lpDsNotify->SetNotificationPositions(2, PositionNotify) != DS_OK){
cout << hr << endl;
cout << "Error while setting up Notification Positions!" << endl;
}
else{
lpDsNotify->Release();
}
}
else{
cout << "Notification settings failed!" << endl;
}
hr = lpdsbuffer->Play(0,0,DSBPLAY_LOOPING);
printf("Buffer set up successfully! Playing back sound... \n \n");
playing = true;
while(playing){
hr = WaitForMultipleObjects(2,NotifyEvent,FALSE,INFINITE);
//cout << hr << endl;
if(hr-WAIT_OBJECT_0 == 0){
cout << "Notify bei 512!" << endl;
ResetEvent(NotifyEvent[0]);
}
else if(hr-WAIT_OBJECT_0 == 1){
cout << "Notify bei 1024!" << endl;
ResetEvent(NotifyEvent[1]);
}
}
system("PAUSE");
lpdsbuffer->Stop();
lpdsbuffer->Release();
lpds->Release();
return 0;
}
I tired creating another directSound object and set its cooperative level in a separate solution but got the same error. Any help is greatly appreciated
Thanks.











