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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
/*! \file
* implementation functions for class qsample
*/
# include <stdint.h>
# include <stdlib.h> // abs
# include "qwavsample.hh"
# include "qexception.hh"
# include "endian.hh"
#ifdef NLS
# include <locale.h>
# include <libintl.h>
/*! \def define _(s)
* \brief gettextize strings
*/
# define _(s) gettext(s)
#else
/*! \def define _(s)
* \brief bypass gettextization
*/
# define _(s) (s)
#endif
qwavsample::qwavsample (qwav *wav, u_int32_t s) {
sample = (union sample*) (wav->getMap()+wav->getOffset(s));
if (wav->getBitsPerSample()==16) {
if ((uintptr_t)sample % sizeof(int16_t) != 0)
throw qexception(__PRETTY_FUNCTION__, _("misaligned 16-bit sample"));
if (wav->getChannels()==2) {
type = STEREO16;
}
else if (wav->getChannels()==1) {
type = MONO16;
}
else
throw qexception(__PRETTY_FUNCTION__,_("unsupported channel value"));
}
else if (wav->getBitsPerSample()==8) {
if (wav->getChannels()==2) {
type = STEREO8;
}
else if (wav->getChannels()==1) {
type = MONO8;
}
else
throw qexception(__PRETTY_FUNCTION__,_("unsupported channel value"));
}
else
throw qexception(__PRETTY_FUNCTION__,_("unsupported bits/sample value"));
#ifdef QVERBOSE
cerr << "sample type " << type << " mapped at address " << this->sample << endl;
#endif
}
qwavsample::qwavsample(caddr_t pointer, u_int32_t channels, u_int32_t bitspersample) {
sample = (union sample*) pointer;
if (bitspersample==16) {
if ((uintptr_t)sample % sizeof(int16_t) != 0)
throw qexception(__PRETTY_FUNCTION__, _("misaligned 16-bit sample"));
if (channels==2) {
type = STEREO16;
return;
}
else if (channels==1) {
type = MONO16;
return;
}
else
throw qexception(__PRETTY_FUNCTION__,_("unsupported channel value"));
}
else if (bitspersample==8) {
if (channels==2) {
type = STEREO8;
return;
}
else if (channels==1) {
type = MONO8;
return;
}
else
throw qexception(__PRETTY_FUNCTION__,_("unsupported channel value"));
}
else
throw qexception(__PRETTY_FUNCTION__,_("unsupported bits/sample value"));
}
qwavsample::qwavsample(caddr_t pointer, enum type t) {
sample = (union sample*) pointer;
type = t;
}
int qwavsample::getLeft() {
switch (type) {
case MONO8: return sample->mono8;
case MONO16: return letohs_s(sample->mono16);
case STEREO8: return sample->stereo8.left;
case STEREO16: return letohs_s(sample->stereo16.left);
}
throw qexception(__PRETTY_FUNCTION__,_("quelcom internal error"));
}
int qwavsample::getRight() {
switch (type) {
case MONO8: return sample->mono8;
case MONO16: return letohs_s(sample->mono16);
case STEREO8: return sample->stereo8.right;
case STEREO16: return letohs_s(sample->stereo16.right);
}
throw qexception(__PRETTY_FUNCTION__,_("quelcom internal error"));
}
void qwavsample::set(int left, int right) {
switch (type) {
case MONO8: sample->mono8=left; break;
case MONO16: sample->mono16=htoles_s(left); break;
case STEREO8: sample->stereo8.left=left;sample->stereo8.right=right; break;
case STEREO16: sample->stereo16.left=htoles_s(left);sample->stereo16.right=htoles_s(right); break;
default:
throw qexception(__PRETTY_FUNCTION__,_("quelcom internal error"));
}
}
bool qwavsample::isSilence(u_int32_t threshold) {
switch (type) {
case MONO8: return (u_int32_t)abs(sample->mono8)<=threshold;
case MONO16: return (u_int32_t)abs(letohs_s(sample->mono16))<=threshold;
case STEREO8:
return ((u_int32_t)abs(sample->stereo8.left)<=threshold) &&
((u_int32_t)abs(sample->stereo8.right)<=threshold);
case STEREO16:
return ((u_int32_t)abs(letohs_s(sample->stereo16.left))<=threshold) &&
((u_int32_t)abs(letohs_s(sample->stereo16.right))<=threshold);
}
throw qexception(__PRETTY_FUNCTION__,_("quelcom internal error"));
}
void qwavsample::setNext() {
switch (type) {
case MONO8: sample = (union sample*)((char*)sample + 1); break;
case MONO16: sample = (union sample*)((uint16_t*)sample + 1); break;
case STEREO8: sample = (union sample*)((struct stereo8*)sample + 1); break;
case STEREO16: sample = (union sample*)((struct stereo16*)sample + 1); break;
default:
throw qexception(__PRETTY_FUNCTION__,_("quelcom internal error"));
}
#ifdef QVERBOSE
cerr << "sample now is mapped at " << (void*) sample << endl;
#endif
}
ostream& operator<<(ostream &os, qwavsample &s) {
s.print(os);
return os;
}
void qwavsample::print(ostream &os) {
switch (type) {
case MONO8: os << '(' << sample->mono8 << ')'; break;
case MONO16: os << '(' << letohs_s(sample->mono16) << ')'; break;
case STEREO8:
os << '(' << sample->stereo8.left << ',' << sample->stereo8.right << ')';
break;
case STEREO16:
os << '(' << letohs_s(sample->stereo16.left) << ',' << letohs_s(sample->stereo16.right) << ')';
break;
default:
throw qexception(__PRETTY_FUNCTION__,_("quelcom internal error"));
}
}
void qwavsample::prod(double d) {
switch (type) {
case MONO8: sample->mono8*=(char)d; break;
case MONO16:
sample->mono16 = htoles_s(short(letohs_s(sample->mono16)*d));
break;
case STEREO8: sample->stereo8.left*=(char)d; sample->stereo8.right*=(char)d; break;
case STEREO16:
sample->stereo16.left = htoles_s(short(letohs_s(sample->stereo16.left)*d));
sample->stereo16.right = htoles_s(short(letohs_s(sample->stereo16.right)*d));
break;
default:
throw qexception(__PRETTY_FUNCTION__,_("quelcom internal error"));
}
}
|