我将不得不使用24位音频数据.这只是unsigned int而不是32bit,它是24bit.那么在C中定义和使用24位数据的简单方法是什么?
Mic*_*kis 15
根据要求,我要么使用位域:
struct int24{
unsigned int data : 24;
};
Run Code Online (Sandbox Code Playgroud)
或者,如果分离更容易,只需使用3个字节unsigned char.如果您不希望填充结构,可以强制打包结构.
[编辑:我看到C++标签已被删除,但无论如何我都将其留在此处]如果您对C++更加满意,可以使用以下内容:
const int INT24_MAX = 8388607;
class Int24
{
protected:
unsigned char value[3];
public:
Int24(){}
Int24( const Int24& val )
{
*this = val;
}
operator int() const
{
/* Sign extend negative quantities */
if( value[2] & 0x80 ) {
return (0xff << 24) | (value[2] << 16)
| (value[1] << 8)
| value[0];
} else {
return (value[2] << 16)
| (value[1] << 8)
| value[0];
}
}
Int24& operator= (const Int24& input)
{
value[0] = input.value[0];
value[1] = input.value[1];
value[2] = input.value[2];
return *this;
}
Int24& operator= (const int input)
{
value[0] = ((unsigned char*)&input)[0];
value[1] = ((unsigned char*)&input)[1];
value[2] = ((unsigned char*)&input)[2];
return *this;
}
Int24 operator+ (const Int24& val) const
{
return Int24( (int)*this + (int)val );
}
Int24 operator- (const Int24& val) const
{
return Int24( (int)*this - (int)val );
}
Int24 operator* (const Int24& val) const
{
return Int24( (int)*this * (int)val );
}
Int24 operator/ (const Int24& val) const
{
return Int24( (int)*this / (int)val );
}
Int24& operator+= (const Int24& val)
{
*this = *this + val;
return *this;
}
Int24& operator-= (const Int24& val)
{
*this = *this - val;
return *this;
}
Int24& operator*= (const Int24& val)
{
*this = *this * val;
return *this;
}
Int24& operator/= (const Int24& val)
{
*this = *this / val;
return *this;
}
Int24 operator>> (const int val) const
{
return Int24( (int)*this >> val );
}
Int24 operator<< (const int val) const
{
return Int24( (int)*this << val );
}
operator bool() const
{
return (int)*this != 0;
}
bool operator! () const
{
return !((int)*this);
}
Int24 operator- ()
{
return Int24( -(int)*this );
}
bool operator== (const Int24& val) const
{
return (int)*this == (int)val;
}
bool operator!= (const Int24& val) const
{
return (int)*this != (int)val;
}
bool operator>= (const Int24& val) const
{
return (int)*this >= (int)val;
}
bool operator<= (const Int24& val) const
{
return (int)*this <= (int)val;
}
/* Define all operations you need below.. */
Run Code Online (Sandbox Code Playgroud)
干净,便携的方式,假设您的样品是小端和无符号的:
static inline uint32_t getsample(uint8_t *buf, size_t pos)
{
return buf[3*pos] + 256UL*buf[3*pos+1] + 65536UL*buf[3*pos+2];
}
static inline void putsample(uint8_t *buf, size_t pos, uint32_t sample)
{
buf[3*pos]=sample;
buf[3*pos+1]=sample/256;
buf[3*pos+2]=sample/65536;
}
Run Code Online (Sandbox Code Playgroud)
修复它以便为签名值工作它会更加有效,特别是如果你想保持它的可移植性.请注意,如果在处理之前将整个样本窗口转换为saner格式,然后在完成后转换回来,性能可能会好得多.
Dan*_*Dan -3
你可以做这样的事情;
union u32to24 {
unsigned int i;
char c[3];
};
Run Code Online (Sandbox Code Playgroud)
u32to24.i在您的代码中使用例如操作值
u32to24 val = //...
val.i += foo
val.i -= bar
// etc
Run Code Online (Sandbox Code Playgroud)
然后输出字符得到24位
fprintf("%c%c%c",val.c[0],val.c[1],val.c[2]);
Run Code Online (Sandbox Code Playgroud)
附带条件:这是我脑海中首先想到的事情,因此可能会有错误,并且很可能有更好的方法来做到这一点。
| 归档时间: |
|
| 查看次数: |
24874 次 |
| 最近记录: |