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 145 146 147
|
#include <ClanLib/core.h>
#include <ClanLib/application.h>
#include <ClanLib/sound.h>
class SinusStream : public CL_StreamSoundProvider_Session
{
public:
SinusStream()
{
pos = 0;
playing = false;
stream_length = 60*44100; // play sound for 60 seconds.
}
virtual bool eof() const
{
return (pos >= stream_length); // only stream for four seconds.
}
virtual void stop()
{
playing = false;
}
virtual bool play()
{
playing = true;
return true;
}
virtual bool set_position(int pos)
{
this->pos = pos;
return true;
}
virtual int get_data(void *data_ptr, int data_requested)
{
int samples_req = data_requested / 2;
if (samples_req + pos > stream_length)
// playing beyond the end of stream data
{
samples_req = stream_length - pos;
if (samples_req < 0)
{
stop();
return 0;
}
}
short *samples = (short *) data_ptr;
// Generate required audio samples
for (int i=0; i<samples_req; i++)
{
samples[i] = (short) (sin(((pos+i)*440*3.14)/44100)*32000);
}
pos += samples_req;
return data_requested; // return the amount samples given.
}
virtual int get_frequency() const
{
return 44100;
}
virtual SoundFormat get_format() const
{
return sf_16bit_signed;
}
private:
int pos;
bool playing;
int stream_length;
};
class SinusStreamProvider : public CL_StreamSoundProvider
{
public:
virtual CL_StreamSoundProvider_Session *begin_session()
{
return new SinusStream();
}
virtual void end_session(CL_StreamSoundProvider_Session *session)
{
delete session;
}
};
class App : public CL_ClanApplication
{
public:
virtual char *get_title() { return "Streamed sinus test"; }
virtual int main(int argc, char **argv)
{
// Create a console window for text-output if not available
CL_ConsoleWindow console("Console");
console.redirect_stdio();
try
{
CL_SetupCore::init();
CL_SetupSound::init();
// Create the soundbuffer with the SinusStreamProvider
CL_SoundBuffer *buffer = CL_SoundBuffer::create(
new SinusStreamProvider,
true);
// Prepare a soundbuffer session
CL_SoundBuffer_Session session(buffer->prepare());
// Start playing
session.play();
// Wait some time
CL_System::keep_alive(5000);
// Stop playback
session.stop();
// Cleanup
delete buffer;
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;
|