home *** CD-ROM | disk | FTP | other *** search
- // PROG13_3.CPP - Loading and playing a voc file
- // with real-time manipulation
-
- // INCLUDES ///////////////////////////////////////////////
- #define WIN32_LEAN_AND_MEAN
- #define INITGUID
-
- #include <windows.h> // include important windows stuff
- #include <windowsx.h>
- #include <mmsystem.h>
- #include <iostream.h> // include important C/C++ stuff
- #include <conio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #include <memory.h>
- #include <string.h>
- #include <stdarg.h>
- #include <stdio.h>
- #include <math.h>
- #include <io.h>
- #include <fcntl.h>
-
- #include <ddraw.h> // directX includes
- #include <dsound.h>
-
- // DEFINES ////////////////////////////////////////////////
-
- // directx 7+ compatibility, MS got rid of this constant, but I like it
- #ifndef DSBCAPS_CTRLDEFAULT
- #define DSBCAPS_CTRLDEFAULT (DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME )
- #endif
-
-
- // defines for windows
- #define WINDOW_CLASS_NAME "WINXCLASS" // class name
-
- #define WINDOW_WIDTH 320 // size of window
- #define WINDOW_HEIGHT 240
- #define SCREEN_WIDTH 640 // size of screen
- #define SCREEN_HEIGHT 480
- #define SCREEN_BPP 8 // bits per pixel
-
- #define NVB_SIZE 6 // size of new voice block in bytes
-
- // MACROS /////////////////////////////////////////////////
-
- // these read the keyboard asynchronously
- #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
- #define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
-
- // TYPES //////////////////////////////////////////////////
-
- typedef unsigned short USHORT;
- typedef unsigned short WORD;
- typedef unsigned char UCHAR;
- typedef unsigned char BYTE;
-
- // this structure holds a loaded sound
- typedef struct VOC_FILE_TYP
- {
- int sample_rate; // sample rate of sound
- int length; // length of sound data
- LPDIRECTSOUNDBUFFER lpdsbuffer; // ptr to sound buffer
- } VOC_FILE, *VOC_FILE_PTR;
-
- // PROTOTYPES /////////////////////////////////////////////
-
- int Game_Init(void *parms=NULL);
- int Game_Shutdown(void *parms=NULL);
- int Game_Main(void *parms=NULL);
- int Load_VOC(char *voc_file, VOC_FILE_PTR voc_data);
-
- // GLOBALS ////////////////////////////////////////////////
-
- HWND main_window_handle = NULL; // save the window handle
- HINSTANCE main_instance = NULL; // save the instance
- char buffer[80]; // used to print text
-
- LPDIRECTSOUND lpds; // directsound interface pointer
- DSBUFFERDESC dsbd; // directsound description
- DSCAPS dscaps; // directsound caps
- HRESULT dsresult; // general directsound result
- DSBCAPS dsbcaps; // directsound buffer caps
-
- LPDIRECTSOUNDBUFFER lpdsbprimary, // you won't need this normally
- lpdsbsecondary; // the sound buffers
-
- WAVEFORMATEX pcmwf; // generic waveformat structure
-
- VOC_FILE work_voc; // a working voc file
-
- HWND freq_hwnd, // window handles for controls
- volume_hwnd,
- pan_hwnd;
-
- // FUNCTIONS //////////////////////////////////////////////
-
- LRESULT CALLBACK WindowProc(HWND hwnd,
- UINT msg,
- WPARAM wparam,
- LPARAM lparam)
- {
- // this is the main message handler of the system
- PAINTSTRUCT ps; // used in WM_PAINT
- HDC hdc; // handle to a device context
-
- // what is the message
- switch(msg)
- {
- case WM_CREATE:
- {
- // do initialization stuff here
- return(0);
- } break;
-
- case WM_PAINT:
- {
- // start painting
- hdc = BeginPaint(hwnd,&ps);
-
- // first the static text
- // set the color
- SetTextColor(hdc,RGB(0,0,255));
- SetBkColor(hdc,RGB(0,0,0));
- SetBkMode(hdc,OPAQUE);
-
- TextOut(hdc,160-5*7,40-20,"VOLUME ",strlen("VOLUME "));
- TextOut(hdc,160-5*13,100-20,"PLAYBACK RATE ",strlen("PLAYBACK RATE "));
- TextOut(hdc,160-5*14,160-20,"STEREO PANNING ",strlen("STEREO PANNING "));
-
- // end painting
- EndPaint(hwnd,&ps);
- return(0);
- } break;
-
- ///////////////////////////////////////////////////////////
-
- case WM_HSCROLL:
- case WM_VSCROLL:
- {
- int nscrollcode = (int)LOWORD(wparam); // scroll bar value
- int npos = (int)HIWORD(wparam); // scroll box position
- HWND hwndscrollbar = (HWND)lparam; // handle of scroll bar
-
- // get teh dc for printing
- hdc = GetDC(hwnd);
-
- // set the color
- SetTextColor(hdc,RGB(0,0,255));
- SetBkColor(hdc,RGB(0,0,0));
- SetBkMode(hdc,OPAQUE);
-
- // make sure that the scroll bar is being tracked
- if (nscrollcode==SB_THUMBPOSITION ||
- nscrollcode==SB_THUMBTRACK)
- {
- // what scroll bar sent message
- if (hwndscrollbar==volume_hwnd)
- {
- // re-position scroll bar
- SetScrollPos(volume_hwnd, SB_CTL,npos,TRUE);
- sprintf(buffer,"VOLUME=%d ",-npos);
-
- // output text
- TextOut(hdc,160-5*7,40-20,buffer,strlen(buffer));
-
- // set the volume
- work_voc.lpdsbuffer->SetVolume(-npos);
-
- } // end if
- else
- if (hwndscrollbar==freq_hwnd)
- {
- // re-position scroll bar
- SetScrollPos(freq_hwnd, SB_CTL,npos,TRUE);
- sprintf(buffer,"PLAYBACK RATE=%d ",npos);
-
- // output text
- TextOut(hdc,160-5*13,100-20,buffer,strlen(buffer));
-
- // set the frequency
- work_voc.lpdsbuffer->SetFrequency(npos);
- } // end if
- // what scroll bar sent message
- else
- if (hwndscrollbar==pan_hwnd)
- {
- // re-position scroll bar
- SetScrollPos(pan_hwnd, SB_CTL,npos,TRUE);
- sprintf(buffer,"STEREO PANNING=%d ",-(npos-10000));
-
- // output text
- TextOut(hdc,160-5*14,160-20,buffer,strlen(buffer));
-
- // set the stereo panning
- work_voc.lpdsbuffer->SetPan(-(npos-10000));
- } // end if
-
- } // end if
-
- // release the dc
- ReleaseDC(hwnd,hdc);
-
- // message has been processed
- return(0);
-
- } break;
-
- ///////////////////////////////////////////////////////////
-
- case WM_DESTROY:
- {
- // kill the application
- PostQuitMessage(0);
- return(0);
- } break;
-
- default:break;
-
- } // end switch
-
- // process any messages that we didn't take care of
- return (DefWindowProc(hwnd, msg, wparam, lparam));
-
- } // end WinProc
-
- // WINMAIN ////////////////////////////////////////////////
-
- int WINAPI WinMain( HINSTANCE hinstance,
- HINSTANCE hprevinstance,
- LPSTR lpcmdline,
- int ncmdshow)
- {
-
- WNDCLASS winclass; // this will hold the class we create
- HWND hwnd; // generic window handle
- MSG msg; // generic message
- HDC hdc; // generic dc
- PAINTSTRUCT ps; // generic paintstruct
-
- // first fill in the window class stucture
- winclass.style = CS_DBLCLKS | CS_OWNDC |
- CS_HREDRAW | CS_VREDRAW;
- winclass.lpfnWndProc = WindowProc;
- winclass.cbClsExtra = 0;
- winclass.cbWndExtra = 0;
- winclass.hInstance = hinstance;
- winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
- winclass.lpszMenuName = NULL;
- winclass.lpszClassName = WINDOW_CLASS_NAME;
-
- // register the window class
- if (!RegisterClass(&winclass))
- return(0);
-
- // create the window, note the use of WS_POPUP
- if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
- "WinX Game Console - 2001", // title
- WS_OVERLAPPEDWINDOW | WS_VISIBLE,
- 0,0, // x,y
- WINDOW_WIDTH, // width
- WINDOW_HEIGHT, // height
- NULL, // handle to parent
- NULL, // handle to menu
- hinstance,// instance
- NULL))) // creation parms
- return(0);
-
- // save the window handle and instance in a global
- main_window_handle = hwnd;
- main_instance = hinstance;
-
- // create some scroll controls to change volume, frequency
- // and panning
-
- // create the volume control scroller
- volume_hwnd = CreateWindow("SCROLLBAR", // class
- "", // title
- WS_CHILD | WS_VISIBLE,
- 80,40, // x,y
- 160, // width
- 16, // height
- hwnd, // handle to parent
- NULL, // handle to menu
- hinstance,// instance
- NULL); // creation parms
-
- // create the frequency control scroller
- freq_hwnd = CreateWindow("SCROLLBAR", // class
- "", // title
- WS_CHILD | WS_VISIBLE,
- 80,100, // x,y
- 160, // width
- 16, // height
- hwnd, // handle to parent
- NULL, // handle to menu
- hinstance,// instance
- NULL); // creation parms
-
- // create the stereo panning control scroller
- pan_hwnd = CreateWindow("SCROLLBAR", // class
- "", // title
- WS_CHILD | WS_VISIBLE,
- 80,160, // x,y
- 160, // width
- 16, // height
- hwnd, // handle to parent
- NULL, // handle to menu
- hinstance,// instance
- NULL); // creation parms
-
-
- // set range and value of each scroll bar
- SetScrollRange(volume_hwnd, SB_CTL, 0, 4000,TRUE);
- SetScrollPos(volume_hwnd, SB_CTL,0,TRUE);
-
- SetScrollRange(freq_hwnd, SB_CTL, 0, 50000,TRUE);
- SetScrollPos(freq_hwnd, SB_CTL,11000,TRUE);
-
- SetScrollRange(pan_hwnd, SB_CTL, 0, 20000,TRUE);
- SetScrollPos(pan_hwnd, SB_CTL,10000,TRUE);
-
- // perform all game console specific initialization
- // start up the directsound sound
- Game_Init();
-
- // enter main event loop
- while(1)
- {
- if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
- {
- // test if this is a quit
- if (msg.message == WM_QUIT)
- break;
-
- // translate any accelerator keys
- TranslateMessage(&msg);
-
- // send the message to the window proc
- DispatchMessage(&msg);
- } // end if
-
- // main game processing goes here
- Game_Main();
-
- } // end while
-
- // shutdown game and release all resources
- Game_Shutdown();
-
- // return to Windows like this
- return(msg.wParam);
-
- } // end WinMain
-
- // WINX GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
-
- int Game_Init(void *parms)
- {
- // this function is where you do all the initialization
- // for your game
-
- // create a directsound object
- if (DirectSoundCreate(NULL, &lpds, NULL)!=DS_OK )
- return(0);
-
- // set cooperation level
- if (lpds->SetCooperativeLevel(main_window_handle,DSSCL_NORMAL)!=DS_OK)
- return(0);
-
- // load a voc file in
- if (Load_VOC("DAVE.VOC",&work_voc))
- {
- // start the voc playing in looping mode
- work_voc.lpdsbuffer->Play(0,0,DSBPLAY_LOOPING);
- } // end if
-
- // return success
- return(1);
-
- } // end Game_Init
-
- ///////////////////////////////////////////////////////////
-
- int Game_Shutdown(void *parms)
- {
- // this function is where you shutdown your game and
- // release all resources that you allocated
-
- // release the sound buffer
- if (work_voc.lpdsbuffer)
- work_voc.lpdsbuffer->Release();
-
- // release the directsoundobject
- if (lpds!=NULL)
- lpds->Release();
-
- // return success
- return(1);
- } // end Game_Shutdown
-
- ///////////////////////////////////////////////////////////
-
- int Game_Main(void *parms)
- {
- // this is the workhorse of your game it will be called
- // continuously in real-time this is like main() in C
- // all the calls for you game go here!
-
- // check of user is trying to exit
- if (KEY_DOWN(VK_ESCAPE) || KEY_DOWN(VK_SPACE))
- PostMessage(main_window_handle, WM_DESTROY,0,0);
-
- // return success
- return(1);
- } // end Game_Main
-
- ///////////////////////////////////////////////////////////
-
- int Load_VOC(char *filename,VOC_FILE_PTR voc_data)
- {
- // this function creates a sound from the send voc_file_ptr
- // description and returns a pointer to it
-
- int index, // looping var
- data_offset, // working var
- playback_rate, // temp playback rate
- data_length; // used to hold data length
-
- ULONG bytesread = 0; // actual size of total file
- HANDLE file_handle; // file handle
- UCHAR *snd_buffer; // temporary sound buffer to hold voc data
-
- UCHAR *audio_ptr_1=NULL, // pointer to lock
- *audio_ptr_2=NULL;
-
- DWORD audio_length_1=0, // lengths to lock
- audio_length_2=0;
-
- // load the voc file off disk
- if (!(file_handle = CreateFile(filename,
- GENERIC_READ, // access (read-write) mode
- 0, // share mode
- NULL, // pointer to security descriptor
- OPEN_EXISTING, // how to create
- FILE_ATTRIBUTE_NORMAL, // file attributes
- NULL // handle to file with attributes to copy
- )))
- return(0);
-
- // allocate a large enough temporary buffer (hold 50 secs)
- snd_buffer = (UCHAR *)malloc(640000);
-
- // now read in the data
- ReadFile(file_handle, snd_buffer, 640000, &bytesread, NULL);
-
- // access all values
- data_offset = snd_buffer[20];
- playback_rate = (-1000000/(snd_buffer[data_offset+4]-256));
- data_length = ((*(int *)(snd_buffer+data_offset)) >> 8);
-
- // set rate and size in data structure
- voc_data->sample_rate = playback_rate;
- voc_data->length = data_length;
-
- // close the file
- CloseHandle(file_handle);
-
- // step three: create the sound buffer and copy voc data into buffer
-
- // set up the format data structure
- memset(&pcmwf, 0, sizeof(WAVEFORMATEX));
-
- pcmwf.wFormatTag = WAVE_FORMAT_PCM;
- pcmwf.nChannels = 1;
- pcmwf.nSamplesPerSec= 11025;
- pcmwf.nBlockAlign = 1;
- pcmwf.nAvgBytesPerSec = pcmwf.nSamplesPerSec * pcmwf.nBlockAlign;
- pcmwf.wBitsPerSample= 8;
- pcmwf.cbSize = 0;
-
- // create the sound buffer
- dsbd.dwSize = sizeof(DSBUFFERDESC);
- dsbd.dwFlags = DSBCAPS_CTRLDEFAULT | DSBCAPS_STATIC | DSBCAPS_LOCSOFTWARE ;
- dsbd.dwBufferBytes = data_length-NVB_SIZE;
- dsbd.lpwfxFormat = &pcmwf;
-
- if (lpds->CreateSoundBuffer(&dsbd,&(voc_data->lpdsbuffer),NULL)!=DS_OK)
- return(0);
-
- // copy data into sound buffer
- if ((voc_data->lpdsbuffer)->Lock(0,
- data_length-NVB_SIZE,
- (void **) &audio_ptr_1,
- &audio_length_1,
- (void **)&audio_ptr_2,
- &audio_length_2,
- DSBLOCK_FROMWRITECURSOR)!=DS_OK)
- return(0);
-
-
- // copy first section of circular buffer
- memcpy(audio_ptr_1, snd_buffer+data_offset+NVB_SIZE, audio_length_1);
-
- // copy last section of circular buffer
- memcpy(audio_ptr_2, (snd_buffer+data_offset+NVB_SIZE+audio_length_1),audio_length_2);
-
- // unlock the buffer
- if ((voc_data->lpdsbuffer)->Unlock(audio_ptr_1,
- audio_length_1,
- audio_ptr_2,
- audio_length_2)!=DS_OK)
- return(0);
-
- // release the temp buffer
- free(snd_buffer);
-
- // return success
- return(1);
-
- } // end Load_Voc