SoundFrame.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005 #include "SoundFrame.h"
00006 #include "JackClient.h"
00007
00008 SoundFrame::SoundFrame(jack_default_audio_sample_t* data, jack_nframes_t count) : m_mallocd(false)
00009 {
00010 Set(data, count);
00011 }
00012
00013 SoundFrame::SoundFrame(jack_nframes_t count) : m_mallocd(false)
00014 {
00015 Set(count);
00016 Zero();
00017 }
00018
00019 SoundFrame::SoundFrame() : m_mallocd(false)
00020 {
00021 Set();
00022 }
00023
00024 void SoundFrame::Zero()
00025 {
00026 memset(m_samples, 0, sizeof(jack_default_audio_sample_t) * m_samplecount);
00027 }
00028
00029 void SoundFrame::Set()
00030 {
00031 if (m_mallocd) {
00032 free(m_samples);
00033 }
00034 m_samplecount = 0;
00035 m_samples = NULL;
00036 m_mallocd = false;
00037 }
00038
00039 void SoundFrame::Set(const SoundFrame& f)
00040 {
00041 Set(f.SampleCount());
00042 memcpy(m_samples, f.GetSamples(), sizeof(jack_default_audio_sample_t) * m_samplecount);
00043 }
00044
00045 void SoundFrame::Set(jack_nframes_t count)
00046 {
00047 if (m_mallocd) {
00048 m_samples = (jack_default_audio_sample_t*)realloc(m_samples, count * sizeof(jack_default_audio_sample_t));
00049 }
00050 else {
00051 Set();
00052
00053 m_samples = (jack_default_audio_sample_t*)malloc(sizeof(jack_default_audio_sample_t) * count);
00054 m_samplecount = count;
00055 Zero();
00056 m_mallocd = true;
00057 }
00058 }
00059
00060 void SoundFrame::Set(jack_default_audio_sample_t* data, jack_nframes_t count)
00061 {
00062 Set(count);
00063 memcpy(m_samples, data, sizeof(jack_default_audio_sample_t) * count);
00064 }
00065
00066 SoundFrame::~SoundFrame()
00067 {
00068 Set();
00069 }
00070
00071 jack_time_t SoundFrame::FrameTime()
00072 {
00073 return jack_frames_to_time(JackClient::GetSingleton()->GetClient(), m_samplecount);
00074 }
00075
00076 jack_time_t SoundFrame::SampleTime(jack_nframes_t offset)
00077 {
00078 return jack_frames_to_time(JackClient::GetSingleton()->GetClient(), offset);
00079 }
00080
00081 SoundFrame& SoundFrame::operator+=(const SoundFrame& f)
00082 {
00083 if (m_samplecount < f.SampleCount()) {
00084 Set(f.SampleCount());
00085 }
00086
00087 for (unsigned int x = 0; x < m_samplecount; ++x) {
00088 m_samples[x] += f.GetSamples()[x];
00089 }
00090
00091 return *this;
00092 }
00093
00094 SoundFrame& SoundFrame::operator*=(const SoundFrame& f)
00095 {
00096 if (m_samplecount < f.SampleCount()) {
00097 Set(f.SampleCount());
00098 }
00099
00100 for (unsigned int x = 0; x < m_samplecount; ++x) {
00101 m_samples[x] *= f.GetSamples()[x];
00102 }
00103
00104 return *this;
00105 }
00106
00107 SoundFrame& SoundFrame::operator*=(float scalar)
00108 {
00109 for (unsigned int x = 0; x < m_samplecount; ++x) {
00110 m_samples[x] *= scalar;
00111 }
00112
00113 return *this;
00114 }
00115
00116 SoundFrame& SoundFrame::operator=(const SoundFrame& f)
00117 {
00118 Set(f);
00119 return *this;
00120 }
00121
00122 SoundFrame operator+(const SoundFrame& f1, const SoundFrame& f2)
00123 {
00124 SoundFrame ret;
00125 ret.Set(f1);
00126
00127 if (f1.SampleCount() < f2.SampleCount()) {
00128 ret.Set(f2.SampleCount());
00129 }
00130
00131 jack_default_audio_sample_t* out = ret.GetSamples();
00132 jack_default_audio_sample_t* sam = f2.GetSamples();
00133 for (unsigned int x = 0; x < ret.SampleCount(); ++x) {
00134 out[x] += sam[x];
00135 }
00136
00137 return ret;
00138 }
00139
00140 SoundFrame operator*(const SoundFrame& f1, const SoundFrame& f2)
00141 {
00142 SoundFrame ret;
00143 ret.Set(f1);
00144
00145 if (f1.SampleCount() < f2.SampleCount()) {
00146 ret.Set(f2.SampleCount());
00147 }
00148
00149 jack_default_audio_sample_t* out = ret.GetSamples();
00150 jack_default_audio_sample_t* sam = f2.GetSamples();
00151 for (unsigned int x = 0; x < ret.SampleCount(); ++x) {
00152 out[x] *= sam[x];
00153 }
00154
00155 return ret;
00156 }
00157
00158 SoundFrame operator*(const SoundFrame& f1, float scalar)
00159 {
00160 SoundFrame ret;
00161 ret.Set(f1);
00162
00163 jack_default_audio_sample_t* out = ret.GetSamples();
00164 for (unsigned int x = 0; x < ret.SampleCount(); ++x) {
00165 out[x] *= scalar;
00166 }
00167
00168 return ret;
00169 }
00170
00171 ostream& operator<<(ostream& o, const SoundFrame& sf)
00172 {
00173 o << sf.SampleCount() << "samples: " << endl;
00174 for (unsigned int x = 0; x < sf.SampleCount(); ++x) {
00175 o << " " << sf.GetSamples()[x];
00176 }
00177 return o;
00178 }
00179