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