Nice way to add sound to my glut project also icons.
Hi, I am currently at a stage where I am ready to add audio to the game I am creating. I am currently using mmsystem and playsound to play my sound, however as you will know this is limited. Whats another way to add sound that I can have more controll over?
Also I am looking for a way to change the icon of the exe I create, how would I do that?
Quote: Original post by Groberts
Hi, I am currently at a stage where I am ready to add audio to the game I am creating. I am currently using mmsystem and playsound to play my sound, however as you will know this is limited. Whats another way to add sound that I can have more controll over?
Also I am looking for a way to change the icon of the exe I create, how would I do that?
You could look at OpenAL for sound, or you can use DirectSound if you plan on staying on Windows and want to use it.
As for an application icon, you use a resource file (or whatever your IDE wants to present you with).
If you're happy with its licensing arrangements, I've always found fmod to be a great product to work with.
--
Cheers,
Darren Clark
Cheers,
Darren Clark
I was just checking out both of them, but it is hard to find out quickly how much code it takes to simply load and play one simple sound. Could you guys let me know?
Quote: Original post by Groberts
I was just checking out both of them, but it is hard to find out quickly how much code it takes to simply load and play one simple sound. Could you guys let me know?
Not mutch, if condensed it only takes about a page or two of code al in all to load and play a sound in openAL (it's about the same for fmod).
To illustrate this i wrote a small class to play sounds in openAL for my 3d engine.
(In full surround glory)
CFsound.cpp
#include <windows.h>#include <stdio.h>#include <stdlib.h>#include <al/al.h>#include <al/alc.h>#include <al/alu.h>#include <al/alut.h>#include <fstream.h>#include "CFsound.h"#include <math.h>#include <iostream>#pragma comment(lib, "alut.lib")#pragma comment(lib, "OpenAL32.lib")static CFsound::buf buffer[255];static CFsound::src source[255];void CFsound::init(void){ alutInit(NULL, 0); alGetError(); int i=0; while(i<256) { buffer.active=0; i++; } i=0; while(i<256) { source.active=0; i++; }}void CFsound::destroy(void){ int i=0; while(i<256) { if(buffer.active==1) {alDeleteBuffers(1, &buffer.buffer); buffer.active=0;} i++; } i=0; while(i<256) { if(source.active==1) {alDeleteSources(1, &source.source); source.active=0;} i++; } alutExit();}int CFsound::getUBuf(void){ int i=0; while(i<256) { if(buffer.active==0) return i; i++; } return 0;}int CFsound::getUSrc(void){ int i=0; while(i<256) { if(source.active==0) return i; i++; } return 0;}int CFsound::loadsound(char filename[69],int loop){ ALenum format; ALsizei size; ALvoid* data; ALsizei freq; int i=0; i=getUBuf(); alGenBuffers(1, &buffer.buffer); alutLoadWAVFile(filename, &format, &data, &size, &freq, &buffer.loop); if (loop!=2) buffer.loop=loop; alBufferData(buffer.buffer, format, data, size, freq); alutUnloadWAV(format, data, size, freq); buffer.active=1; return i;}void CFsound::deleteBuffer(unsigned int b){ alDeleteBuffers(1, &buffer.buffer); buffer.active=0;}int CFsound::createSource(unsigned int b){ int i=0; i=getUSrc(); alGenSources(1, &source.source); alSourcei (source.source, AL_BUFFER, buffer.buffer ); alSourcef (source.source, AL_PITCH, 1.0f ); alSourcef (source.source, AL_GAIN, 4.0f ); alSourcei (source.source, AL_LOOPING, buffer.loop ); source.active=1; return i;}void CFsound::setListner(float p[3], float v[3], float fu[6]){ alListenerfv(AL_POSITION, p); alListenerfv(AL_VELOCITY, v); alListenerfv(AL_ORIENTATION, fu);}void CFsound::setSoundPos(unsigned int s, double x, double y, double z, double vx, double vy, double vz){ float p[3]; int error; p[0]=x; p[1]=y; p[2]=z; alGetError(); alSourcefv(source.source,AL_POSITION,p);<br><br> error=alGetError();<br><span class="cpp-comment">// if (error == AL_NO_ERROR ) MessageBox(NULL,"AL_NO_ERROR","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);</span><br> <span class="cpp-keyword">if</span> (error == AL_INVALID_NAME ) MessageBox(NULL,<span class="cpp-literal">"AL_INVALID_NAME."</span>,<span class="cpp-literal">"SHUTDOWN ERROR"</span>,MB_OK | MB_ICONINFORMATION);<br> <span class="cpp-keyword">if</span> (error == AL_INVALID_ENUM ) MessageBox(NULL,<span class="cpp-literal">"AL_INVALID_ENUM."</span>,<span class="cpp-literal">"SHUTDOWN ERROR"</span>,MB_OK | MB_ICONINFORMATION);<br> <span class="cpp-keyword">if</span> (error == AL_INVALID_VALUE ) MessageBox(NULL,<span class="cpp-literal">"AL_INVALID_VALUE "</span>,<span class="cpp-literal">"SHUTDOWN ERROR"</span>,MB_OK | MB_ICONINFORMATION);<br> <span class="cpp-keyword">if</span> (error == AL_INVALID_OPERATION ) MessageBox(NULL,<span class="cpp-literal">"AL_INVALID_OPERATION ."</span>,<span class="cpp-literal">"SHUTDOWN ERROR"</span>,MB_OK | MB_ICONINFORMATION);<br> <span class="cpp-keyword">if</span> (error == AL_OUT_OF_MEMORY ) MessageBox(NULL,<span class="cpp-literal">"AL_OUT_OF_MEMORY."</span>,<span class="cpp-literal">"SHUTDOWN ERROR"</span>,MB_OK | MB_ICONINFORMATION);<br><br> alSource3f(source.source,AL_VELOCITY, vx, vy, vz);<br><br> error=alGetError();<br><span class="cpp-comment">// if (error == AL_NO_ERROR ) MessageBox(NULL,"AL_NO_ERROR","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);</span><br> <span class="cpp-keyword">if</span> (error == AL_INVALID_NAME ) MessageBox(NULL,<span class="cpp-literal">"AL_INVALID_NAME."</span>,<span class="cpp-literal">"SHUTDOWN ERROR"</span>,MB_OK | MB_ICONINFORMATION);<br> <span class="cpp-keyword">if</span> (error == AL_INVALID_ENUM ) MessageBox(NULL,<span class="cpp-literal">"AL_INVALID_ENUM."</span>,<span class="cpp-literal">"SHUTDOWN ERROR"</span>,MB_OK | MB_ICONINFORMATION);<br> <span class="cpp-keyword">if</span> (error == AL_INVALID_VALUE ) MessageBox(NULL,<span class="cpp-literal">"AL_INVALID_VALUE "</span>,<span class="cpp-literal">"SHUTDOWN ERROR"</span>,MB_OK | MB_ICONINFORMATION);<br> <span class="cpp-keyword">if</span> (error == AL_INVALID_OPERATION ) MessageBox(NULL,<span class="cpp-literal">"AL_INVALID_OPERATION ."</span>,<span class="cpp-literal">"SHUTDOWN ERROR"</span>,MB_OK | MB_ICONINFORMATION);<br> <span class="cpp-keyword">if</span> (error == AL_OUT_OF_MEMORY ) MessageBox(NULL,<span class="cpp-literal">"AL_OUT_OF_MEMORY."</span>,<span class="cpp-literal">"SHUTDOWN ERROR"</span>,MB_OK | MB_ICONINFORMATION);<br><br> <br><br><br><br><br>}<br><br><span class="cpp-keyword">void</span> CFsound::play(<span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">int</span> s)<br>{<br><br> alGetError();<br><br> alSourcePlay(source.source);<br><br> <span class="cpp-keyword">int</span> error;<br> error=alGetError();<br><span class="cpp-comment">// if (error == AL_NO_ERROR ) MessageBox(NULL,"AL_NO_ERROR","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);</span><br> <span class="cpp-keyword">if</span> (error == AL_INVALID_NAME ) MessageBox(NULL,<span class="cpp-literal">"AL_INVALID_NAME."</span>,<span class="cpp-literal">"SHUTDOWN ERROR"</span>,MB_OK | MB_ICONINFORMATION);<br> <span class="cpp-keyword">if</span> (error == AL_INVALID_ENUM ) MessageBox(NULL,<span class="cpp-literal">"AL_INVALID_ENUM."</span>,<span class="cpp-literal">"SHUTDOWN ERROR"</span>,MB_OK | MB_ICONINFORMATION);<br> <span class="cpp-keyword">if</span> (error == AL_INVALID_VALUE ) MessageBox(NULL,<span class="cpp-literal">"AL_INVALID_VALUE "</span>,<span class="cpp-literal">"SHUTDOWN ERROR"</span>,MB_OK | MB_ICONINFORMATION);<br> <span class="cpp-keyword">if</span> (error == AL_INVALID_OPERATION ) MessageBox(NULL,<span class="cpp-literal">"AL_INVALID_OPERATION ."</span>,<span class="cpp-literal">"SHUTDOWN ERROR"</span>,MB_OK | MB_ICONINFORMATION);<br> <span class="cpp-keyword">if</span> (error == AL_OUT_OF_MEMORY ) MessageBox(NULL,<span class="cpp-literal">"AL_OUT_OF_MEMORY."</span>,<span class="cpp-literal">"SHUTDOWN ERROR"</span>,MB_OK | MB_ICONINFORMATION);<br><br><br><br>}<br><br><span class="cpp-keyword">void</span> CFsound::pause(<span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">int</span> s)<br>{<br> alSourcePause(source.source);<br>}<br><br><span class="cpp-keyword">void</span> CFsound::stop(<span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">int</span> s)<br>{<br> alSourceStop(source.source);<br>}<br><br><span class="cpp-keyword">void</span> CFsound::rewind(<span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">int</span> s)<br>{<br> alSourceRewind(source.source);<br>}<br><br><br></pre></div><!–ENDSCRIPT–><br><br><br>and CFsound.h<br><!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre><br><br><span class="cpp-directive">#include</span> <windows.h> <span class="cpp-comment">// Standard windows header</span><br><span class="cpp-directive">#include</span> <stdio.h> <br><span class="cpp-directive">#include</span> <al/al.h><br><span class="cpp-directive">#include</span> <al/alc.h><br><span class="cpp-directive">#include</span> <al/alu.h><br><span class="cpp-directive">#include</span> <al/alut.h><br><span class="cpp-directive">#include</span> <fstream><br><br><span class="cpp-keyword">class</span> CFsound {<br><br> <span class="cpp-keyword">public</span>:<br><br> <span class="cpp-keyword">typedef</span> <span class="cpp-keyword">struct</span><br> {<br> ALuint buffer;<br> ALboolean loop;<br> <span class="cpp-keyword">int</span> active;<br> } buf;<br><br> <span class="cpp-keyword">typedef</span> <span class="cpp-keyword">struct</span><br> {<br> ALuint source;<br> <span class="cpp-keyword">int</span> active;<br> } src;<br><br> <span class="cpp-keyword">int</span> getUBuf(<span class="cpp-keyword">void</span>);<br> <span class="cpp-keyword">int</span> getUSrc(<span class="cpp-keyword">void</span>);<br><br> <br> <br> <span class="cpp-keyword">void</span> init(<span class="cpp-keyword">void</span>);<br> <span class="cpp-keyword">void</span> destroy(<span class="cpp-keyword">void</span>);<br><br> <span class="cpp-keyword">int</span> loadsound(<span class="cpp-keyword">char</span> filename[<span class="cpp-number">69</span>],<span class="cpp-keyword">int</span> loop);<br> <span class="cpp-keyword">int</span> createSource(<span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">int</span> b);<br> <span class="cpp-keyword">void</span> deleteBuffer(<span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">int</span> b);<br> <span class="cpp-keyword">void</span> setListner(<span class="cpp-keyword">float</span> p[<span class="cpp-number">3</span>], <span class="cpp-keyword">float</span> v[<span class="cpp-number">3</span>], <span class="cpp-keyword">float</span> fu[<span class="cpp-number">6</span>]);<br> <span class="cpp-keyword">void</span> setSoundPos(<span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">int</span> s, <span class="cpp-keyword">double</span> x, <span class="cpp-keyword">double</span> y, <span class="cpp-keyword">double</span> z, <span class="cpp-keyword">double</span> vx, <span class="cpp-keyword">double</span> vy, <span class="cpp-keyword">double</span> vz);<br><br> <span class="cpp-keyword">void</span> play(<span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">int</span> s);<br> <span class="cpp-keyword">void</span> pause(<span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">int</span> s);<br> <span class="cpp-keyword">void</span> stop(<span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">int</span> s);<br> <span class="cpp-keyword">void</span> rewind(<span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">int</span> s);<br> <br><br>};<br><br><br></pre></div><!–ENDSCRIPT–><br>and finaly you use it<br><br><!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre><br>CFsound sn;<br><span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">int</span> b,s;<br><br>sn.init();<br><br>b=sn.loadsound(<span class="cpp-literal">"funk.wav"</span>,<span class="cpp-number">1</span>);<br>s=sn.createSource(b);<br>sn.setSoundPos(s,<span class="cpp-number">0</span>,<span class="cpp-number">0</span>,<span class="cpp-number">0</span> ,<span class="cpp-number">0</span>,<span class="cpp-number">0</span>,<span class="cpp-number">0</span>);<br><span class="cpp-keyword">float</span> listenerPos[]={<span class="cpp-number">0</span>.<span class="cpp-number">0</span>,<span class="cpp-number">0</span>.<span class="cpp-number">0</span>,<span class="cpp-number">0</span>.<span class="cpp-number">0</span>};<br><span class="cpp-keyword">float</span> listenerVel[]={<span class="cpp-number">0</span>.<span class="cpp-number">0</span>,<span class="cpp-number">0</span>.<span class="cpp-number">0</span>,<span class="cpp-number">0</span>.<span class="cpp-number">0</span>};<br><span class="cpp-keyword">float</span> listenerOri[]={<span class="cpp-number">0</span>.<span class="cpp-number">0</span>,<span class="cpp-number">0</span>.<span class="cpp-number">0</span>,<span class="cpp-number">1</span>.<span class="cpp-number">0</span>, <span class="cpp-number">0</span>.<span class="cpp-number">0</span>,<span class="cpp-number">1</span>.<span class="cpp-number">0</span>,<span class="cpp-number">0</span>.<span class="cpp-number">0</span>};<br>sn.setListner(listenerPos,listenerVel,listenerOri);<br><br>sn.play(s);<br>sn.stop(s);<br><br>sn.destroy();<br><br></pre></div><!–ENDSCRIPT–><br><br>it's not hard, you just have to figure it out atleast once for yourself.
www.flashbang.se | www.thegeekstate.com | nehe.gamedev.net | glAux fix for lesson 6 | [twitter]thegeekstate[/twitter]
I have had no luck in working out how to use any of those libaries so I am back to using playsound. Now this would be perfect for me if there was anyway to play more than one sound at once.
PlaySound("ugh.wav",0,SND_FILENAME|SND_ASYNC|SND_NODEFAULT);
I am currently using that line. Is there any way to get this to do what I want or is it hopeless?
PlaySound("ugh.wav",0,SND_FILENAME|SND_ASYNC|SND_NODEFAULT);
I am currently using that line. Is there any way to get this to do what I want or is it hopeless?
i would say that playsound is pretty limited, if you want more than that then you must use one of the other APIs.
fmod and bass is probobly the easiest ones to work with and openAL is a bit more for puritans, but it only took me about 5-6 hours to get the class i put up here up and running, and about 20 min to get fmod to play a tune for the minidemo contest in 2003, just search for tutorials about them and you'll be set.
tut's for openAL
http://www.devmaster.net/articles.php?catID=6
http://www.gamedev.net/reference/articles/article2008.asp
fmod and bass is probobly the easiest ones to work with and openAL is a bit more for puritans, but it only took me about 5-6 hours to get the class i put up here up and running, and about 20 min to get fmod to play a tune for the minidemo contest in 2003, just search for tutorials about them and you'll be set.
tut's for openAL
http://www.devmaster.net/articles.php?catID=6
http://www.gamedev.net/reference/articles/article2008.asp
www.flashbang.se | www.thegeekstate.com | nehe.gamedev.net | glAux fix for lesson 6 | [twitter]thegeekstate[/twitter]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement