effects_internal.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. SDL_mixer: An audio mixer library based on the SDL library
  3. Copyright (C) 1997-2013 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. This file by Ryan C. Gordon ([email protected])
  18. These are some helper functions for the internal mixer special effects.
  19. */
  20. /* $Id$ */
  21. /* ------ These are used internally only. Don't touch. ------ */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include "SDL_mixer.h"
  25. #define __MIX_INTERNAL_EFFECT__
  26. #include "effects_internal.h"
  27. /* Should we favor speed over memory usage and/or quality of output? */
  28. int _Mix_effects_max_speed = 0;
  29. void _Mix_InitEffects(void)
  30. {
  31. _Mix_effects_max_speed = (SDL_getenv(MIX_EFFECTSMAXSPEED) != NULL);
  32. }
  33. void _Mix_DeinitEffects(void)
  34. {
  35. _Eff_PositionDeinit();
  36. }
  37. void *_Eff_volume_table = NULL;
  38. /* Build the volume table for Uint8-format samples.
  39. *
  40. * Each column of the table is a possible sample, while each row of the
  41. * table is a volume. Volume is a Uint8, where 0 is silence and 255 is full
  42. * volume. So _Eff_volume_table[128][mysample] would be the value of
  43. * mysample, at half volume.
  44. */
  45. void *_Eff_build_volume_table_u8(void)
  46. {
  47. int volume;
  48. int sample;
  49. Uint8 *rc;
  50. if (!_Mix_effects_max_speed) {
  51. return(NULL);
  52. }
  53. if (!_Eff_volume_table) {
  54. rc = SDL_malloc(256 * 256);
  55. if (rc) {
  56. _Eff_volume_table = (void *) rc;
  57. for (volume = 0; volume < 256; volume++) {
  58. for (sample = -128; sample < 128; sample ++) {
  59. *rc = (Uint8)(((float) sample) * ((float) volume / 255.0))
  60. + 128;
  61. rc++;
  62. }
  63. }
  64. }
  65. }
  66. return(_Eff_volume_table);
  67. }
  68. /* Build the volume table for Sint8-format samples.
  69. *
  70. * Each column of the table is a possible sample, while each row of the
  71. * table is a volume. Volume is a Uint8, where 0 is silence and 255 is full
  72. * volume. So _Eff_volume_table[128][mysample+128] would be the value of
  73. * mysample, at half volume.
  74. */
  75. void *_Eff_build_volume_table_s8(void)
  76. {
  77. int volume;
  78. int sample;
  79. Sint8 *rc;
  80. if (!_Eff_volume_table) {
  81. rc = SDL_malloc(256 * 256);
  82. if (rc) {
  83. _Eff_volume_table = (void *) rc;
  84. for (volume = 0; volume < 256; volume++) {
  85. for (sample = -128; sample < 128; sample ++) {
  86. *rc = (Sint8)(((float) sample) * ((float) volume / 255.0));
  87. rc++;
  88. }
  89. }
  90. }
  91. }
  92. return(_Eff_volume_table);
  93. }
  94. /* end of effects.c ... */