00001 #include "repeatable.hpp"
00002
00003
00005
00006 RepeatableStream::RepeatableStream(ISampleSource* source, bool initial_state)
00007 {
00008 m_repeat = initial_state;
00009 m_source = source;
00010
00011
00012 int channel_count, sample_rate, bits_per_sample;
00013 m_source->GetFormat(channel_count, sample_rate, bits_per_sample);
00014 m_sample_size = channel_count * bits_per_sample / 8;
00015 }
00016
00018
00019 RepeatableStream::~RepeatableStream()
00020 {
00021 delete m_source;
00022 }
00023
00025
00026 void
00027 RepeatableStream::SetRepeat(bool repeat)
00028 {
00029 m_repeat = repeat;
00030 }
00031
00033
00034 bool
00035 RepeatableStream::GetRepeat()
00036 {
00037 return m_repeat;
00038 }
00039
00041
00042 void
00043 RepeatableStream::GetFormat(
00044 int& channel_count,
00045 int& sample_rate,
00046 int& bits_per_sample)
00047 {
00048 m_source->GetFormat(channel_count, sample_rate, bits_per_sample);
00049 }
00050
00052
00053 int
00054 RepeatableStream::Read(int sample_count, void* samples)
00055 {
00056 if (m_repeat) {
00057
00058 unsigned char* out = (unsigned char*)samples;
00059 int samples_left = sample_count;
00060 while (samples_left > 0) {
00061
00062
00063 int samples_read = m_source->Read(samples_left, out);
00064
00065
00066 if (samples_read == 0) {
00067 m_source->Reset();
00068 samples_read = m_source->Read(samples_left, samples);
00069 }
00070
00071
00072 if (samples_read == 0) {
00073 break;
00074 }
00075
00076 samples_left -= samples_read;
00077 out += samples_read * m_sample_size;
00078 }
00079
00080 return sample_count - samples_left;
00081
00082 } else {
00083
00084 return m_source->Read(sample_count, samples);
00085
00086 }
00087 }
00088
00090
00091 bool
00092 RepeatableStream::Reset()
00093 {
00094 return m_source->Reset();
00095 }
00096