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 148 149 150 151 152 153 154 155
|
#include <ClanLib/core.h>
#include <ClanLib/application.h>
#include <ClanLib/sound.h>
class SinusStream : public CL_SoundProvider_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 int get_position() const
{
return pos;
}
virtual bool set_position(int new_pos)
{
pos = new_pos;
return true;
}
virtual int get_num_samples() const
{
return stream_length;
}
virtual int get_data(void **data_ptr, int samples_req)
{
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 *channel1 = (short *) data_ptr[0];
// Generate required audio samples
for (int i=0; i<samples_req; i++)
{
channel1[i] = (short) (sin(((pos+i)*440*3.14)/44100)*32000);
}
pos += samples_req;
return samples_req; // return the amount samples given.
}
virtual int get_frequency() const
{
return 44100;
}
virtual CL_SoundFormat get_format() const
{
return sf_16bit_signed;
}
virtual int get_num_channels() const
{
return 1;
}
private:
int pos;
bool playing;
int stream_length;
};
class SinusStreamProvider : public CL_SoundProvider
{
public:
virtual CL_SoundProvider_Session *begin_session()
{
return new SinusStream();
}
virtual void end_session(CL_SoundProvider_Session *session)
{
delete session;
}
};
class App : public CL_ClanApplication
{
public:
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 setup_core;
CL_SetupSound setup_sound;
CL_SoundOutput output(44100);
// Create the soundbuffer with the SinusStreamProvider
CL_SoundBuffer buffer(
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();
}
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;
|