4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "byteorder.h"
28 int64_t FileBase::samples_to_raw(char *out_buffer,
36 int output_advance; // number of bytes in a sample
37 float *buffer_channel; // channel in input buffer
38 float *buffer_channel_end;
40 int64_t int_sample, int_sample2;
42 int64_t dither_value, dither_scale = 255;
43 int64_t bytes = input_len * channels * (file->bytes_per_sample(bits));
44 int machine_byte_order = get_byte_order();
50 char *output_ptr, *output_end;
51 output_advance = channels;
52 for(channel = 0; channel < channels; channel++)
54 output_ptr = out_buffer + channel;
55 buffer_channel = in_buffer[channel];
56 buffer_channel_end = buffer_channel + input_len;
60 for( ; buffer_channel < buffer_channel_end; buffer_channel++)
62 float_sample = *buffer_channel * 0x7fff;
63 int_sample = (int64_t)float_sample;
64 if(int_sample > -0x7f00) { dither_value = rand() % dither_scale; int_sample -= dither_value; }
65 int_sample /= 0x100; // rotating bits screws up the signs
66 *output_ptr = int_sample;
67 output_ptr += output_advance;
72 for( ; buffer_channel < buffer_channel_end; buffer_channel++)
74 float_sample = *buffer_channel * 0x7f;
75 *output_ptr = (char)float_sample;
76 output_ptr += output_advance;
84 output_ptr = out_buffer;
85 output_end = out_buffer + bytes;
87 for( ; output_ptr < output_end; output_ptr++)
96 output_advance = channels;
97 for(channel = 0; channel < channels; channel++)
99 output_ptr = (int16_t*)out_buffer + channel;
100 buffer_channel = in_buffer[channel];
101 buffer_channel_end = buffer_channel + input_len;
105 for( ; buffer_channel < buffer_channel_end; buffer_channel++)
107 float_sample = *buffer_channel * 0x7fffff;
108 int_sample = (int64_t)float_sample;
109 if(int_sample > -0x7fff00) { dither_value = rand() % dither_scale; int_sample -= dither_value; }
111 *output_ptr = int_sample;
112 output_ptr += output_advance;
117 for( ; buffer_channel < buffer_channel_end; buffer_channel++)
119 float_sample = *buffer_channel * 0x7fff;
120 *output_ptr = (int16_t)float_sample;
121 output_ptr += output_advance;
131 output_advance = asset->channels * 3 - 2;
132 for(channel = 0; channel < channels; channel++)
134 output_ptr = out_buffer + channel * 3;
135 buffer_channel = in_buffer[channel];
136 buffer_channel_end = buffer_channel + input_len;
138 // don't bother dithering 24 bits
139 for( ; buffer_channel < buffer_channel_end; buffer_channel++)
141 float_sample = *buffer_channel * 0x7fffff;
142 int_sample = (int64_t)float_sample;
143 int_sample2 = int_sample & 0xff0000;
144 *output_ptr++ = (int_sample & 0xff);
145 int_sample &= 0xff00;
146 *output_ptr++ = (int_sample >> 8);
147 *output_ptr = (int_sample2 >> 16);
148 output_ptr += output_advance;
157 output_advance = asset->channels;
158 //printf("FileBase::samples_to_raw 1\n");
159 generate_ulaw_tables();
160 //printf("FileBase::samples_to_raw 2\n");
162 for(channel = 0; channel < channels; channel++)
164 output_ptr = out_buffer + channel;
165 buffer_channel = in_buffer[channel];
166 buffer_channel_end = buffer_channel + input_len;
167 for( ; buffer_channel < buffer_channel_end; buffer_channel++)
169 *output_ptr = floattoulaw(*buffer_channel);
170 output_ptr += output_advance;
173 //printf("FileBase::samples_to_raw 3\n");
179 if((bits == BITSLINEAR16 && byte_order != machine_byte_order) ||
180 (bits == BITSLINEAR24 && !byte_order))
182 swap_bytes(file->bytes_per_sample(bits), (unsigned char*)out_buffer, bytes);
189 #define READ_8_MACRO \
190 sample = *inbuffer_8; \
192 inbuffer_8 += input_frame;
194 #define READ_16_MACRO \
195 sample = *inbuffer_16; \
197 inbuffer_16 += input_frame;
199 #define READ_24_MACRO \
200 sample = (unsigned char)*inbuffer_24++; \
201 sample_24 = (unsigned char)*inbuffer_24++; \
203 sample += sample_24; \
204 sample_24 = *inbuffer_24; \
206 sample += sample_24; \
207 sample /= 0x7fffff; \
208 inbuffer_24 += input_frame; \
210 #define READ_ULAW_MACRO \
211 sample = ulawtofloat(*inbuffer_8); \
212 inbuffer_8 += input_frame;
214 #define LFEATHER_MACRO1 \
215 for(feather_current = 0; feather_current < lfeather_len; \
216 output_current++, feather_current++) \
219 #define LFEATHER_MACRO2 \
220 current_gain = lfeather_gain + lfeather_slope * feather_current; \
221 out_buffer[output_current] = out_buffer[output_current] * (1 - current_gain) + sample * current_gain; \
224 #define CENTER_MACRO1 \
225 for(; output_current < samples; \
229 #define CENTER_MACRO2 \
230 out_buffer[output_current] += sample; \
233 int FileBase::raw_to_samples(float *out_buffer, char *in_buffer,
234 int64_t samples, int bits, int channels, int channel, int feather,
235 float lfeather_len, float lfeather_gain, float lfeather_slope)
237 int64_t output_current = 0; // position in output buffer
238 int64_t input_len = samples; // length of input buffer
239 // The following are floats because they are multiplied by the slope to get the gain.
240 float feather_current; // input position for feather
243 char *inbuffer_8 = 0; // point to actual byte being read
244 int16_t *inbuffer_16 = 0;
245 char *inbuffer_24 = 0;
248 int input_frame = 0; // amount to advance the input buffer pointer
250 // set up the parameters
254 inbuffer_8 = in_buffer + channel;
255 input_frame = channels;
259 inbuffer_16 = (int16_t *)in_buffer + channel;
260 input_frame = channels;
264 inbuffer_24 = in_buffer + channel * 3;
265 input_frame = channels * file->bytes_per_sample(bits) - 2;
269 generate_ulaw_tables();
270 inbuffer_8 = in_buffer + channel;
271 input_frame = channels;
276 // ================== calculate feathering and add to buffer ================
337 // ====================== don't feather and overwrite buffer =================
342 for(; output_current < input_len;
344 { READ_8_MACRO; out_buffer[output_current] = sample; }
348 for(; output_current < input_len;
350 { READ_16_MACRO; out_buffer[output_current] = sample; }
354 for(; output_current < input_len;
356 { READ_24_MACRO; out_buffer[output_current] = sample; }
360 for(; output_current < input_len;
362 { READ_ULAW_MACRO; out_buffer[output_current] = sample; }
370 int FileBase::overlay_float_buffer(float *out_buffer, float *in_buffer,
372 float lfeather_len, float lfeather_gain, float lfeather_slope)
374 int64_t output_current = 0;
375 float sample, current_gain;
376 float feather_current; // input position for feather
379 sample = in_buffer[output_current];
383 sample = in_buffer[output_current];
390 int FileBase::get_audio_buffer(char **buffer, int64_t len, int64_t bits, int64_t channels)
392 int64_t bytes = len * channels * (file->bytes_per_sample(bits));
393 if(*buffer && bytes > prev_bytes)
400 if(!*buffer) *buffer = new char[bytes];
404 int FileBase::get_float_buffer(float **buffer, int64_t len)
406 if(*buffer && len > prev_len)
413 if(!*buffer) *buffer = new float[len];