7 int64_t FileBase::samples_to_raw(char *out_buffer,
15 int output_advance; // number of bytes in a sample
16 float *buffer_channel; // channel in input buffer
17 float *buffer_channel_end;
19 int64_t int_sample, int_sample2;
21 int64_t dither_value, dither_scale = 255;
22 int64_t bytes = input_len * channels * (file->bytes_per_sample(bits));
23 int machine_byte_order = get_byte_order();
29 char *output_ptr, *output_end;
30 output_advance = channels;
31 for(channel = 0; channel < channels; channel++)
33 output_ptr = out_buffer + channel;
34 buffer_channel = in_buffer[channel];
35 buffer_channel_end = buffer_channel + input_len;
39 for( ; buffer_channel < buffer_channel_end; buffer_channel++)
41 float_sample = *buffer_channel * 0x7fff;
42 int_sample = (int64_t)float_sample;
43 if(int_sample > -0x7f00) { dither_value = rand() % dither_scale; int_sample -= dither_value; }
44 int_sample /= 0x100; // rotating bits screws up the signs
45 *output_ptr = int_sample;
46 output_ptr += output_advance;
51 for( ; buffer_channel < buffer_channel_end; buffer_channel++)
53 float_sample = *buffer_channel * 0x7f;
54 *output_ptr = (char)float_sample;
55 output_ptr += output_advance;
63 output_ptr = out_buffer;
64 output_end = out_buffer + bytes;
66 for( ; output_ptr < output_end; output_ptr++)
74 int16_t *output_ptr, *output_end;
75 output_advance = channels;
76 for(channel = 0; channel < channels; channel++)
78 output_ptr = (int16_t*)out_buffer + channel;
79 buffer_channel = in_buffer[channel];
80 buffer_channel_end = buffer_channel + input_len;
84 for( ; buffer_channel < buffer_channel_end; buffer_channel++)
86 float_sample = *buffer_channel * 0x7fffff;
87 int_sample = (int64_t)float_sample;
88 if(int_sample > -0x7fff00) { dither_value = rand() % dither_scale; int_sample -= dither_value; }
90 *output_ptr = int_sample;
91 output_ptr += output_advance;
96 for( ; buffer_channel < buffer_channel_end; buffer_channel++)
98 float_sample = *buffer_channel * 0x7fff;
99 *output_ptr = (int16_t)float_sample;
100 output_ptr += output_advance;
109 char *output_ptr, *output_end;
110 output_advance = asset->channels * 3 - 2;
111 for(channel = 0; channel < channels; channel++)
113 output_ptr = out_buffer + channel * 3;
114 buffer_channel = in_buffer[channel];
115 buffer_channel_end = buffer_channel + input_len;
117 // don't bother dithering 24 bits
118 for( ; buffer_channel < buffer_channel_end; buffer_channel++)
120 float_sample = *buffer_channel * 0x7fffff;
121 int_sample = (int64_t)float_sample;
122 int_sample2 = int_sample & 0xff0000;
123 *output_ptr++ = (int_sample & 0xff);
124 int_sample &= 0xff00;
125 *output_ptr++ = (int_sample >> 8);
126 *output_ptr = (int_sample2 >> 16);
127 output_ptr += output_advance;
136 output_advance = asset->channels;
137 //printf("FileBase::samples_to_raw 1\n");
138 generate_ulaw_tables();
139 //printf("FileBase::samples_to_raw 2\n");
141 for(channel = 0; channel < channels; channel++)
143 output_ptr = out_buffer + channel;
144 buffer_channel = in_buffer[channel];
145 buffer_channel_end = buffer_channel + input_len;
146 for( ; buffer_channel < buffer_channel_end; buffer_channel++)
148 *output_ptr = floattoulaw(*buffer_channel);
149 output_ptr += output_advance;
152 //printf("FileBase::samples_to_raw 3\n");
158 if((bits == BITSLINEAR16 && byte_order != machine_byte_order) ||
159 (bits == BITSLINEAR24 && !byte_order))
161 swap_bytes(file->bytes_per_sample(bits), (unsigned char*)out_buffer, bytes);
168 #define READ_8_MACRO \
169 sample = *inbuffer_8; \
171 inbuffer_8 += input_frame;
173 #define READ_16_MACRO \
174 sample = *inbuffer_16; \
176 inbuffer_16 += input_frame;
178 #define READ_24_MACRO \
179 sample = (unsigned char)*inbuffer_24++; \
180 sample_24 = (unsigned char)*inbuffer_24++; \
182 sample += sample_24; \
183 sample_24 = *inbuffer_24; \
185 sample += sample_24; \
186 sample /= 0x7fffff; \
187 inbuffer_24 += input_frame; \
189 #define READ_ULAW_MACRO \
190 sample = ulawtofloat(*inbuffer_8); \
191 inbuffer_8 += input_frame;
193 #define LFEATHER_MACRO1 \
194 for(feather_current = 0; feather_current < lfeather_len; \
195 output_current++, feather_current++) \
198 #define LFEATHER_MACRO2 \
199 current_gain = lfeather_gain + lfeather_slope * feather_current; \
200 out_buffer[output_current] = out_buffer[output_current] * (1 - current_gain) + sample * current_gain; \
203 #define CENTER_MACRO1 \
204 for(; output_current < samples; \
208 #define CENTER_MACRO2 \
209 out_buffer[output_current] += sample; \
212 int FileBase::raw_to_samples(float *out_buffer, char *in_buffer,
213 int64_t samples, int bits, int channels, int channel, int feather,
214 float lfeather_len, float lfeather_gain, float lfeather_slope)
216 int64_t output_current = 0; // position in output buffer
217 int64_t input_len = samples; // length of input buffer
218 // The following are floats because they are multiplied by the slope to get the gain.
219 float feather_current; // input position for feather
222 char *inbuffer_8; // point to actual byte being read
223 int16_t *inbuffer_16;
227 int input_frame; // amount to advance the input buffer pointer
229 // set up the parameters
233 inbuffer_8 = in_buffer + channel;
234 input_frame = channels;
238 inbuffer_16 = (int16_t *)in_buffer + channel;
239 input_frame = channels;
243 inbuffer_24 = in_buffer + channel * 3;
244 input_frame = channels * file->bytes_per_sample(bits) - 2;
248 generate_ulaw_tables();
249 inbuffer_8 = in_buffer + channel;
250 input_frame = channels;
255 // ================== calculate feathering and add to buffer ================
316 // ====================== don't feather and overwrite buffer =================
321 for(; output_current < input_len;
323 { READ_8_MACRO; out_buffer[output_current] = sample; }
327 for(; output_current < input_len;
329 { READ_16_MACRO; out_buffer[output_current] = sample; }
333 for(; output_current < input_len;
335 { READ_24_MACRO; out_buffer[output_current] = sample; }
339 for(; output_current < input_len;
341 { READ_ULAW_MACRO; out_buffer[output_current] = sample; }
349 int FileBase::overlay_float_buffer(float *out_buffer, float *in_buffer,
351 float lfeather_len, float lfeather_gain, float lfeather_slope)
353 int64_t output_current = 0;
354 float sample, current_gain;
355 float feather_current; // input position for feather
358 sample = in_buffer[output_current];
362 sample = in_buffer[output_current];
369 int FileBase::get_audio_buffer(char **buffer, int64_t len, int64_t bits, int64_t channels)
371 int64_t bytes = len * channels * (file->bytes_per_sample(bits));
372 if(*buffer && bytes > prev_bytes)
379 if(!*buffer) *buffer = new char[bytes];
382 int FileBase::get_float_buffer(float **buffer, int64_t len)
384 if(*buffer && len > prev_len)
391 if(!*buffer) *buffer = new float[len];