| 286 | // (16 bits). |
| 287 | |
| 288 | static short MadFixedToShort(mad_fixed_t fixed) |
| 289 | { |
| 290 | // A fixed point number is formed of the following bit pattern: |
| 291 | |
| 292 | // |
| 293 | // SWWWFFFFFFFFFFFFFFFFFFFFFFFFFFFF |
| 294 | // MSB LSB |
| 295 | // S ==> Sign (0 is positive, 1 is negative) |
| 296 | // W ==> Whole part bits |
| 297 | // F ==> Fractional part bits |
| 298 | // |
| 299 | // This pattern contains MAD_F_FRACBITS fractional bits, one should alway |
| 300 | // use this macro when working on the bits of a fixed point number. It is |
| 301 | // not guaranteed to be constant over the different platforms supported by |
| 302 | // libmad. |
| 303 | // |
| 304 | // The signed short value is formed, after clipping, by the least |
| 305 | // significant whole part bit, followed by the 15 most significant |
| 306 | // fractional part bits. Warning: this is a quick and dirty way to compute |
| 307 | // the 16-bit number, madplay includes much better algorithms. |
| 308 | |
| 309 | // Clipping |
| 310 | if (fixed >= MAD_F_ONE) { |
| 311 | return SHRT_MAX; |
| 312 | } |
| 313 | |
| 314 | if (fixed <= -MAD_F_ONE) { |
| 315 | return -SHRT_MAX; |
| 316 | } |
| 317 | |
| 318 | // Conversion |
| 319 | fixed >>= (MAD_F_FRACBITS - 15); |
| 320 | |
| 321 | return static_cast<short>(fixed); |
| 322 | } |
| 323 | |
| 324 | //------------------------------------------------------------------------------ |
| 325 | |