| 1 | #ifndef PA_DITHER_H |
| 2 | #define PA_DITHER_H |
| 3 | /* |
| 4 | * $Id: pa_dither.h,v 1.1 2006/06/10 21:30:55 dmazzoni Exp $ |
| 5 | * Portable Audio I/O Library triangular dither generator |
| 6 | * |
| 7 | * Based on the Open Source API proposed by Ross Bencina |
| 8 | * Copyright (c) 1999-2002 Phil Burk, Ross Bencina |
| 9 | * |
| 10 | * Permission is hereby granted, free of charge, to any person obtaining |
| 11 | * a copy of this software and associated documentation files |
| 12 | * (the "Software"), to deal in the Software without restriction, |
| 13 | * including without limitation the rights to use, copy, modify, merge, |
| 14 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 15 | * and to permit persons to whom the Software is furnished to do so, |
| 16 | * subject to the following conditions: |
| 17 | * |
| 18 | * The above copyright notice and this permission notice shall be |
| 19 | * included in all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * Any person wishing to distribute modifications to the Software is |
| 22 | * requested to send the modifications to the original developer so that |
| 23 | * they can be incorporated into the canonical version. |
| 24 | * |
| 25 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 26 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 27 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 28 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 29 | * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 30 | * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 31 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 32 | */ |
| 33 | |
| 34 | /** @file |
| 35 | @brief Functions for generating dither noise |
| 36 | */ |
| 37 | |
| 38 | |
| 39 | #ifdef __cplusplus |
| 40 | extern "C" |
| 41 | { |
| 42 | #endif /* __cplusplus */ |
| 43 | |
| 44 | |
| 45 | /** @brief State needed to generate a dither signal */ |
| 46 | typedef struct PaUtilTriangularDitherGenerator{ |
| 47 | unsigned long previous; |
| 48 | unsigned long randSeed1; |
| 49 | unsigned long randSeed2; |
| 50 | } PaUtilTriangularDitherGenerator; |
| 51 | |
| 52 | |
| 53 | /** @brief Initialize dither state */ |
| 54 | void PaUtil_InitializeTriangularDitherState( PaUtilTriangularDitherGenerator *ditherState ); |
| 55 | |
| 56 | |
| 57 | /** |
| 58 | @brief Calculate 2 LSB dither signal with a triangular distribution. |
| 59 | Ranged for adding to a 1 bit right-shifted 32 bit integer |
| 60 | prior to >>15. eg: |
| 61 | <pre> |
| 62 | signed long in = * |
| 63 | signed long dither = PaUtil_Generate16BitTriangularDither( ditherState ); |
| 64 | signed short out = (signed short)(((in>>1) + dither) >> 15); |
| 65 | </pre> |
| 66 | @return |
| 67 | A signed long with a range of +32767 to -32768 |
| 68 | */ |
| 69 | signed long PaUtil_Generate16BitTriangularDither( PaUtilTriangularDitherGenerator *ditherState ); |
| 70 | |
| 71 | |
| 72 | /** |
| 73 | @brief Calculate 2 LSB dither signal with a triangular distribution. |
| 74 | Ranged for adding to a pre-scaled float. |
| 75 | <pre> |
| 76 | float in = * |
| 77 | float dither = PaUtil_GenerateFloatTriangularDither( ditherState ); |
| 78 | // use smaller scaler to prevent overflow when we add the dither |
| 79 | signed short out = (signed short)(in*(32766.0f) + dither ); |
| 80 | </pre> |
| 81 | @return |
| 82 | A float with a range of -2.0 to +1.99999. |
| 83 | */ |
| 84 | float PaUtil_GenerateFloatTriangularDither( PaUtilTriangularDitherGenerator *ditherState ); |
| 85 | |
| 86 | |
| 87 | |
| 88 | #ifdef __cplusplus |
| 89 | } |
| 90 | #endif /* __cplusplus */ |
| 91 | #endif /* PA_DITHER_H */ |