1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
/*
Example of writing your own static soundprovider.
*/
#include <ClanLib/core.h>
#include <ClanLib/application.h>
#include <ClanLib/sound.h>
#include <math.h>
class OurStaticSoundProvider : public CL_StaticSoundProvider
{
public:
OurStaticSoundProvider();
virtual ~OurStaticSoundProvider();
virtual void lock();
virtual void unlock();
virtual SoundFormat get_format() const;
virtual int data_size() const;
virtual void *get_data() const;
virtual int get_frequency() const;
private:
int lock_refs;
short *buffer;
};
class StaticSoundProviderApp : public CL_ClanApplication
{
public:
virtual char *get_title() { return "StaticSoundProvider application"; }
virtual int main(int, char **)
{
// Create a console window for text-output if not available
CL_ConsoleWindow console("Console");
console.redirect_stdio();
try
{
CL_SetupCore::init();
CL_SetupSound::init();
CL_SoundBuffer *soundbuffer = CL_SoundBuffer::create(
new OurStaticSoundProvider,
true); // true -> delete provider when soundbuffer is deleted
soundbuffer->play(true);
int i=0;
while (i != 40)
{
// Update keyboard input and handle system events:
// Exits the loop if ClanLib requests shutdown - for instance if
// someone closes the window.
CL_System::keep_alive();
CL_System::sleep(50);
i++;
}
delete soundbuffer;
CL_SetupSound::deinit();
CL_SetupCore::deinit();
}
catch (CL_Error err)
{
std::cout << "Error: " << err.message.c_str() << std::endl;
// Display console close message and wait for a key
console.display_close_message();
return -1;
}
return 0;
}
} app;
#define buffer_size 22050*10
OurStaticSoundProvider::OurStaticSoundProvider()
{
lock_refs = 0;
buffer = NULL;
}
OurStaticSoundProvider::~OurStaticSoundProvider()
{
delete[] buffer;
}
void OurStaticSoundProvider::lock()
{
// Check reference counting
if (lock_refs == 0)
{
// First time creation of buffer
buffer = new short[buffer_size];
// Calculate static sound
for (int i=0; i<buffer_size; i++)
{
buffer[i] = (short) (sin(i*6.28/100)*32000);
}
}
lock_refs++;
}
void OurStaticSoundProvider::unlock()
{
lock_refs--;
// Check reference counting
if (lock_refs == 0)
{
// Cleanup when no references
delete[] buffer;
buffer = NULL;
}
}
SoundFormat OurStaticSoundProvider::get_format() const
{
return sf_8bit_signed_stereo;
}
int OurStaticSoundProvider::data_size() const
{
return sizeof(short)*buffer_size;
}
void *OurStaticSoundProvider::get_data() const
{
return buffer;
}
int OurStaticSoundProvider::get_frequency() const
{
return 22050;
}
|