00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00019
00020 #ifndef header_static_provider_raw
00021 #define header_static_provider_raw
00022
00023 #include <iostream>
00024 #include "../static_soundprovider.h"
00025 #include "../soundbuffer.h"
00026
00027 class CL_Sample_RawData : public CL_StaticSoundProvider
00028
00029 {
00030 public:
00031 static CL_SoundBuffer *create(
00032 unsigned char *sound_data,
00033 int data_size,
00034 int bytes_per_sample,
00035 bool stereo,
00036 int frequency=22050)
00037 {
00038 return CL_SoundBuffer::create(
00039 new CL_Sample_RawData(
00040 sound_data,
00041 data_size,
00042 bytes_per_sample,
00043 stereo,
00044 frequency),
00045 true);
00046 }
00047
00048 CL_Sample_RawData(
00049 unsigned char *sound_data,
00050 int data_size,
00051 int bytes_per_sample,
00052 bool stereo,
00053 int frequency=22050)
00054 {
00055 load_data(
00056 sound_data,
00057 data_size,
00058 bytes_per_sample,
00059 stereo,
00060 frequency);
00061 }
00062
00063 virtual ~CL_Sample_RawData()
00064 {
00065 delete[] sample_data;
00066 }
00067
00068 virtual void lock()
00069 {
00070 return;
00071 }
00072
00073 virtual void unlock()
00074 {
00075
00076
00077 }
00078
00079 virtual SoundFormat get_format() const
00080 {
00081 return sample_format;
00082 }
00083
00084 virtual int data_size() const
00085 {
00086 return sample_size;
00087 }
00088
00089 virtual void *get_data() const
00090 {
00091 return sample_data;
00092 }
00093
00094 virtual int get_frequency() const
00095 {
00096 return sample_freq;
00097 }
00098 private:
00099 struct WAVE_FORMAT
00100 {
00101 short formatTag;
00102 short nChannels;
00103 int nSamplesPerSec;
00104 int nAvgBytesPerSec;
00105 short nBlockAlign;
00106 };
00107
00108 unsigned char *sample_data;
00109 SoundFormat sample_format;
00110 int sample_size;
00111 int sample_freq;
00112
00113 void load_data(
00114 unsigned char *sound_data,
00115 int data_size,
00116 int bytes_per_sample,
00117 bool stereo,
00118 int frequency=22050)
00119 {
00120
00121
00122 sample_size = data_size;
00123 sample_freq = frequency;
00124
00125 sample_data = new unsigned char[sample_size];
00126 memcpy(sample_data, sound_data, sample_size);
00127
00128 if (stereo && bytes_per_sample == 2) sample_format = sf_16bit_signed_stereo;
00129 else if (stereo && bytes_per_sample == 1) sample_format = sf_8bit_signed_stereo;
00130 else if (!stereo && bytes_per_sample == 2) sample_format = sf_16bit_signed;
00131 else if (!stereo && bytes_per_sample == 1) sample_format = sf_8bit_signed;
00132 else
00133 {
00134 std::cout << "Invalid sample format" << std::endl;
00135 exit(1);
00136
00137 }
00138 }
00139 };
00140
00141 #endif