output.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. TiMidity -- Experimental MIDI to WAVE converter
  3. Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. output.c
  16. Audio output (to file / device) functions.
  17. */
  18. #include "config.h"
  19. #include "output.h"
  20. #include "tables.h"
  21. #ifdef SDL
  22. extern PlayMode sdl_play_mode;
  23. #define DEFAULT_PLAY_MODE &sdl_play_mode
  24. #endif
  25. PlayMode *play_mode_list[] = {
  26. #ifdef DEFAULT_PLAY_MODE
  27. DEFAULT_PLAY_MODE,
  28. #endif
  29. 0
  30. };
  31. #ifdef DEFAULT_PLAY_MODE
  32. PlayMode *play_mode=DEFAULT_PLAY_MODE;
  33. #endif
  34. /*****************************************************************/
  35. /* Some functions to convert signed 32-bit data to other formats */
  36. void s32tos8(void *dp, int32_t *lp, int32_t c)
  37. {
  38. int8_t *cp=(int8_t *)(dp);
  39. int32_t l;
  40. while (c--)
  41. {
  42. l=(*lp++)>>(32-8-GUARD_BITS);
  43. if (l>127) l=127;
  44. else if (l<-128) l=-128;
  45. *cp++ = (int8_t) (l);
  46. }
  47. }
  48. void s32tou8(void *dp, int32_t *lp, int32_t c)
  49. {
  50. uint8_t *cp=(uint8_t *)(dp);
  51. int32_t l;
  52. while (c--)
  53. {
  54. l=(*lp++)>>(32-8-GUARD_BITS);
  55. if (l>127) l=127;
  56. else if (l<-128) l=-128;
  57. *cp++ = 0x80 ^ ((uint8_t) l);
  58. }
  59. }
  60. void s32tos16(void *dp, int32_t *lp, int32_t c)
  61. {
  62. int16_t *sp=(int16_t *)(dp);
  63. int32_t l;
  64. while (c--)
  65. {
  66. l=(*lp++)>>(32-16-GUARD_BITS);
  67. if (l > 32767) l=32767;
  68. else if (l<-32768) l=-32768;
  69. *sp++ = (int16_t)(l);
  70. }
  71. }
  72. void s32tou16(void *dp, int32_t *lp, int32_t c)
  73. {
  74. uint16_t *sp=(uint16_t *)(dp);
  75. int32_t l;
  76. while (c--)
  77. {
  78. l=(*lp++)>>(32-16-GUARD_BITS);
  79. if (l > 32767) l=32767;
  80. else if (l<-32768) l=-32768;
  81. *sp++ = 0x8000 ^ (uint16_t)(l);
  82. }
  83. }
  84. void s32tos16x(void *dp, int32_t *lp, int32_t c)
  85. {
  86. int16_t *sp=(int16_t *)(dp);
  87. int32_t l;
  88. while (c--)
  89. {
  90. l=(*lp++)>>(32-16-GUARD_BITS);
  91. if (l > 32767) l=32767;
  92. else if (l<-32768) l=-32768;
  93. *sp++ = XCHG_SHORT((int16_t)(l));
  94. }
  95. }
  96. void s32tou16x(void *dp, int32_t *lp, int32_t c)
  97. {
  98. uint16_t *sp=(uint16_t *)(dp);
  99. int32_t l;
  100. while (c--)
  101. {
  102. l=(*lp++)>>(32-16-GUARD_BITS);
  103. if (l > 32767) l=32767;
  104. else if (l<-32768) l=-32768;
  105. *sp++ = XCHG_SHORT(0x8000 ^ (uint16_t)(l));
  106. }
  107. }
  108. void s32toulaw(void *dp, int32_t *lp, int32_t c)
  109. {
  110. uint8_t *up=(uint8_t *)(dp);
  111. int32_t l;
  112. while (c--)
  113. {
  114. l=(*lp++)>>(32-13-GUARD_BITS);
  115. if (l > 4095) l=4095;
  116. else if (l<-4096) l=-4096;
  117. *up++ = _l2u[l];
  118. }
  119. }