Debian Codegraph @

sndlib/mixer.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <cstdlib>
20 #include <memory.h>
21
22 #include "mono.h"
23 #include "ssl_lib.h"
24 #include "mixer.h"
25 #include "pserror.h"
26
27 #define MIN_SOUND_MIX_VOLUME 0.0f
28 #define MAX_WRITE_AHEAD 0.04f // Seconds to write ahead of the play position (in seconds)
29 #define VOLUME_FIX_BITS 1024
30
31 static inline void opti_8m_mix(uint8_t *cur_sample_8bit, const int num_write, int &samples_played,
32 int16_t *mixer_buffer16, const float l_volume, const float r_volume);
33 static inline void opti_8s_mix(uint8_t *cur_sample_8bit, const int num_write, int &samples_played,
34 int16_t *mixer_buffer16, const float l_volume, const float r_volume);
35 static inline void opti_16m_mix(int16_t *cur_sample_16bit, const int num_write, int &samples_played,
36 int16_t *mixer_buffer16, const float l_volume, const float r_volume);
37 static inline void opti_16s_mix(int16_t *cur_sample_16bit, const int num_write, int &samples_played,
38 int16_t *mixer_buffer16, const float l_volume, const float r_volume);
39
40 software_mixer::software_mixer() {
41 m_init = false;
42 m_buffer = NULL;
43 }
44
45 software_mixer::~software_mixer() {
46 if (m_buffer) {
47 free(m_buffer);
48 }
49 }
50
51 bool software_mixer::Initialize(tMixerInit *mi) {
52 if (m_init) {
53 mprintf(0, "Mixer: Already Init\n");
54 Int3();
55 return false;
56 }
57
58 // Sam 6/29 - When using SDL, there's no primary buffer
59 // if(!mi->primary_buffer || !mi->ll_sound_ptr)
60 if (!mi->ll_sound_ptr) {
61 mprintf(0, "Mixer: Bad Value passed on init\n");
62 Int3();
63 return false;
64 }
65
66 m_init = true;
67
68 m_ll_sound_ptr = mi->ll_sound_ptr;
69 m_primary_buffer = mi->primary_buffer;
70
71 if (m_primary_buffer) {
72 m_BufferSize = m_primary_buffer->GetNumBufferBytes();
73 } else {
74 m_BufferSize = 0;
75 }
76
77 m_max_sounds_available = mi->max_sounds_available;
78 m_sound_cache = mi->sound_cache;
79 m_primary_alignment = mi->primary_alignment;
80
81 m_error_code = mi->p_error_code;
82 m_fpSetError = mi->fp_SetError;
83 m_fpErrorText = mi->fp_ErrorText;
84
85 if (m_BufferSize) {
86 m_buffer = (uint8_t *)malloc(m_BufferSize);
87 }
88
89 return true;
90 }
91
92 // A peroidic mixer that uses the primary buffer as a stream buffer
93 void software_mixer::DoFrame(void) {
94 if (!m_init)
95 return;
96
97 int amount = m_BufferSize;
98
99 StreamMixer((char *)m_buffer, amount);
100 m_primary_buffer->Write(m_buffer, amount);
101 }
102
103 // The actual mixer code that sum's the sounds on each channel and does all the actual
104 // mixing and effects (writes data to the locked primary buffer)
105 void software_mixer::StreamMixer(char *ptr, int len) {
106 int i;
107 int16_t *mixer_buffer16 = (int16_t *)ptr;
108 int current_slot = 0;
109 bool f_loop;
110 bool f_mono;
111
112 const int buff_len = len / m_primary_alignment;
113
114 // this code will assure that this function will not be called when sound system is in error mode.
115 if (*m_error_code != SSL_OK) {
116 mprintf(0, "MIX: Mixer in error code %d\n", *m_error_code);
117 return;
118 }
119
120 // ASSERT(len <= m_BufferSize);
121 ASSERT(ptr && len >= 0);
122
123 if ((len % m_primary_alignment) != 0) {
124 mprintf(0, "MIX:Len is not aligned!\n");
125 (*m_fpSetError)(SSL_ERROR_STREAMMIXER);
126 (*m_fpErrorText)("ASSERT((len % ll_sound_ptr->m_primary_alignment) == 0)\nLen:%d PrA:%d", len, m_primary_alignment);
127 return;
128 }
129
130 memset((char *)ptr, 0, len);
131
132 // Mix the sound slots
133 while (current_slot < (*m_max_sounds_available)) {
134 sound_buffer_info *cur_buf = &m_sound_cache[current_slot];
135 int num_samples = buff_len;
136 mixer_buffer16 = (int16_t *)ptr;
137 f_mono = true;
138
139 // Find slots with sounds in them
140 if ((cur_buf->m_status != SSF_UNUSED) && !(cur_buf->m_status & SSF_PAUSED)) {
141 float l_volume = cur_buf->play_info->left_volume;
142 float r_volume = cur_buf->play_info->right_volume;
143 int skip_interval = cur_buf->play_info->sample_skip_interval;
144 int samples_played = cur_buf->play_info->m_samples_played;
145 int16_t *sample_16bit;
146 uint8_t *sample_8bit;
147 int np_sample_length = 0;
148 int sample_length;
149 int loop_start;
150 int loop_end;
151
152 if (cur_buf->m_status & SSF_PLAY_STREAMING) {
153 switch (cur_buf->play_info->m_stream_format) {
154 case SIF_STREAMING_16_M:
155 sample_16bit = (int16_t *)cur_buf->play_info->m_stream_data;
156 sample_8bit = NULL;
157 np_sample_length = sample_length = cur_buf->play_info->m_stream_size / 2;
158 break;
159 case SIF_STREAMING_8_M:
160 sample_16bit = NULL;
161 sample_8bit = (uint8_t *)cur_buf->play_info->m_stream_data;
162 np_sample_length = sample_length = cur_buf->play_info->m_stream_size;
163 break;
164 case SIF_STREAMING_16_S:
165 sample_16bit = (int16_t *)cur_buf->play_info->m_stream_data;
166 sample_8bit = NULL;
167 np_sample_length = sample_length = cur_buf->play_info->m_stream_size / 4;
168 f_mono = false;
169 break;
170 case SIF_STREAMING_8_S:
171 sample_16bit = NULL;
172 sample_8bit = (uint8_t *)cur_buf->play_info->m_stream_data;
173 np_sample_length = sample_length = cur_buf->play_info->m_stream_size / 2;
174 f_mono = false;
175 break;
176 default:
177 Int3();
178 break;
179 }
180 loop_start = 0;
181 loop_end = np_sample_length - 1;
182 } else {
183 int sound_index = cur_buf->m_sound_index;
184 int sample_index = Sounds[sound_index].sample_index;
185 sound_file_info *snd_file = &SoundFiles[sample_index];
186 sample_16bit = snd_file->sample_16bit;
187 sample_8bit = snd_file->sample_8bit;
188 np_sample_length = snd_file->np_sample_length;
189 sample_length = snd_file->sample_length;
190 loop_start = Sounds[sound_index].loop_start;
191 loop_end = Sounds[sound_index].loop_end;
192 if (!sample_16bit && !sample_8bit) {
193 mprintf(0, "sound file %s didn't have data for samples.\n", snd_file->name);
194 }
195 }
196
197 // cleanly continue if this happens, and inform a logfile, if it does. error handling
198 // ASSERT(sample_16bit || sample_8bit);
199 if (!sample_16bit && !sample_8bit) {
200 sound_file_info *snd_file = &SoundFiles[Sounds[cur_buf->m_sound_index].sample_index];
201 mprintf(0, "MIX: No data for %s\n", snd_file->name);
202 (*m_fpSetError)(SSL_ERROR_SAMPLE_NODATA);
203 (*m_fpErrorText)("ASSERT(sample_16bit || sample_8bit)\nNo data found for sound file: %s", snd_file->name);
204 cur_buf->m_status = SSF_UNUSED;
205 goto error_bail;
206 }
207
208 ASSERT(np_sample_length <= sample_length);
209 ASSERT(samples_played >= 0 || samples_played <= sample_length);
210
211 int num_write;
212
213 error_trap:
214
215 // We have not looped -- yet
216 f_loop = false;
217
218 // Verify the volume levels are o.k.
219 ASSERT(l_volume >= 0.0 && l_volume <= 1.0);
220 ASSERT(r_volume >= 0.0 && r_volume <= 1.0);
221
222 looping: // Will go to this label to do the next iteration of a looping sample
223
224 if (cur_buf->m_status &
225 (SSF_PLAY_LOOPING | SSF_PLAY_STREAMING)) // Looping sample's process is broken up into linear pieces
226 {
227 if (f_loop) {
228 ASSERT(num_write >= 0);
229
230 num_samples -= num_write;
231 ASSERT(num_samples > 0);
232
233 mixer_buffer16 += num_write << 1; // update to the new start position
234 // (2x because of left and right channels)
235 samples_played = loop_start;
236 }
237
238 if (cur_buf->m_status & SSF_PLAY_LOOPING) // Looping sample's process is broken up into linear pieces
239 {
240 ASSERT(loop_end < sample_length);
241 if (loop_end < samples_played) {
242 if (loop_end != loop_start) {
243 while (loop_end < samples_played) {
244 samples_played -= loop_end - loop_start;
245 }
246
247 ASSERT(samples_played >= 0);
248 } else {
249 cur_buf->m_status &= ~SSF_PLAY_LOOPING;
250 cur_buf->m_status |= SSF_PLAY_NORMAL;
251 }
252
253 goto error_trap;
254 }
255 }
256 // The number of samples to write to the primary buffer
257 num_write = ((num_samples) < (loop_end - samples_played + 1)) ? (num_samples) : (loop_end - samples_played + 1);
258 ASSERT(num_write >= 0 && num_write <= num_samples);
259
260 if (num_write < num_samples)
261 f_loop = true;
262 else
263 f_loop = false;
264
265 if (num_write <= 0) {
266 num_write = 0;
267 mprintf(0, "d");
268 goto stream_done;
269 }
270 } else {
271 // The number of samples to write to the primary buffer
272 num_write =
273 ((num_samples) < (np_sample_length - samples_played)) ? (num_samples) : (np_sample_length - samples_played);
274 if (!(num_write > 0 && num_write <= num_samples)) {
275 num_write = 0;
276 goto done;
277 }
278
279 // Optimization for silent sounds
280 if (l_volume <= MIN_SOUND_MIX_VOLUME && r_volume <= MIN_SOUND_MIX_VOLUME) {
281 cur_buf->play_info->m_samples_played += num_write;
282 goto done;
283 }
284 }
285
286 if ((num_write < 0) || (num_write > num_samples)) // this was an assert
287 {
288 num_write = 0;
289 mprintf(0, "D");
290 goto done;
291 }
292
293 // Mix at 16 bits per sample
294 if (skip_interval == 0) {
295 int16_t *cur_sample_16bit = sample_16bit;
296 uint8_t *cur_sample_8bit = sample_8bit;
297
298 if (f_mono) {
299 if (sample_8bit) {
300 cur_sample_8bit += samples_played;
301 opti_8m_mix(cur_sample_8bit, num_write, samples_played, mixer_buffer16, l_volume, r_volume);
302 } else {
303 cur_sample_16bit += samples_played;
304 opti_16m_mix(cur_sample_16bit, num_write, samples_played, mixer_buffer16, l_volume, r_volume);
305 }
306 } else {
307 if (sample_8bit) {
308 cur_sample_8bit += (samples_played << 1);
309 opti_8s_mix(cur_sample_8bit, num_write, samples_played, mixer_buffer16, l_volume, r_volume);
310 } else {
311 cur_sample_16bit += (samples_played << 1);
312 opti_16s_mix(cur_sample_16bit, num_write, samples_played, mixer_buffer16, l_volume, r_volume);
313 }
314 }
315 } else
316 // Account for lower-sampling rate
317 {
318 if (skip_interval == 1) {
319 for (i = 0; i < (num_write << 1); i += 2) {
320 int sample;
321
322 if (sample_16bit) {
323 if (samples_played & 0x0001) {
324 sample = ((int)sample_16bit[samples_played ^ 0x0001] + (int)sample_16bit[samples_played + 1]) >> 1;
325 } else
326 sample = sample_16bit[samples_played];
327 } else {
328 if (samples_played & 0x0001) {
329 // Notes: (<<7) is from a (<<8) - (>>1)
330 // Notes: (-256) is from (-128) + (-128)
331 sample = ((int)sample_8bit[samples_played ^ 0x0001] + (int)sample_8bit[samples_played + 1] - 256) << 7;
332 } else
333 sample = (((int)sample_8bit[samples_played]) - (int)128) << 8;
334 }
335
336 samples_played++;
337
338 ASSERT(i >= 0 && (i + 1 < num_samples * 2));
339
340 int l_sample = mixer_buffer16[i] + (sample * l_volume);
341 int r_sample = mixer_buffer16[i + 1] + (sample * r_volume);
342
343 if (l_sample < -32767)
344 l_sample = -32767;
345 if (l_sample > 32767)
346 l_sample = 32767;
347 if (r_sample < -32767)
348 r_sample = -32767;
349 if (r_sample > 32767)
350 r_sample = 32767;
351
352 mixer_buffer16[i] = (int16_t)l_sample;
353 mixer_buffer16[i + 1] = (int16_t)r_sample;
354 }
355 } else {
356 for (i = 0; i < (num_write << 1); i += 2) {
357 int sample;
358 const int mod_pos = samples_played % 4;
359
360 if (sample_16bit) {
361 switch (mod_pos) {
362 case 0:
363 sample = sample_16bit[samples_played];
364 break;
365 case 1:
366 sample = (sample_16bit[samples_played - 1] * 3 + sample_16bit[samples_played + 3]) >> 2;
367 break;
368 case 2:
369 sample = (sample_16bit[samples_played - 2] + sample_16bit[samples_played + 2]) >> 1;
370 break;
371 case 3:
372 sample = (sample_16bit[samples_played - 3] + sample_16bit[samples_played + 1] * 3) >> 2;
373 break;
374 }
375 } else {
376 switch (mod_pos) {
377 case 0:
378 sample = ((((int)sample_8bit[samples_played]) - 128) << 8);
379 break;
380 case 1:
381 sample = (((((int)sample_8bit[samples_played - 1]) - 128) << 8) * 3 +
382 ((((int)sample_8bit[samples_played + 3]) - 128) << 8)) >>
383 2;
384 break;
385 case 2:
386 sample = (((((int)sample_8bit[samples_played - 2]) - 128) << 8) +
387 ((((int)sample_8bit[samples_played + 2]) - 128) << 8)) >>
388 1;
389 break;
390 case 3:
391 sample = (((((int)sample_8bit[samples_played - 3]) - 128) << 8) +
392 ((((int)sample_8bit[samples_played + 1]) - 128) << 8) * 3) >>
393 2;
394 break;
395 }
396 }
397
398 samples_played++;
399
400 ASSERT(i >= 0 && (i + 1 < num_samples * 2));
401
402 int l_sample = mixer_buffer16[i] + (sample * l_volume);
403 int r_sample = mixer_buffer16[i + 1] + (sample * r_volume);
404
405 if (l_sample < -32767)
406 l_sample = -32767;
407 if (l_sample > 32767)
408 l_sample = 32767;
409 if (r_sample < -32767)
410 r_sample = -32767;
411 if (r_sample > 32767)
412 r_sample = 32767;
413
414 mixer_buffer16[i] = (int16_t)l_sample;
415 mixer_buffer16[i + 1] = (int16_t)r_sample;
416 }
417 }
418 }
419
420 stream_done:
421
422 cur_buf->play_info->m_samples_played = samples_played;
423
424 if (cur_buf->m_status & SSF_PLAY_STREAMING) {
425 if (f_loop) {
426 if (cur_buf->play_info->m_stream_cback && cur_buf->play_info->m_stream_data) {
427 cur_buf->play_info->m_stream_data = (*cur_buf->play_info->m_stream_cback)(
428 cur_buf->play_info->user_data, cur_buf->play_info->m_stream_handle, &cur_buf->play_info->m_stream_size);
429 // cur_buf->s->current_position = (char*)cur_buf->play_info->m_stream_data;
430 // mprintf(0, "%x %d\n", cur_buf->play_info->m_stream_data, cur_buf->play_info->m_stream_size));
431 ASSERT(!(cur_buf->play_info->m_stream_data && cur_buf->play_info->m_stream_size <= 0));
432 // mprintf(0, "Data %X, length %d\n", cur_buf->play_info->m_stream_data, cur_buf->play_info->m_stream_size);
433
434 if (cur_buf->play_info->m_stream_data) {
435 switch (cur_buf->play_info->m_stream_format) {
436 case SIF_STREAMING_16_M:
437 sample_16bit = (int16_t *)cur_buf->play_info->m_stream_data;
438 loop_end = sample_length = np_sample_length = cur_buf->play_info->m_stream_size / 2;
439 break;
440 case SIF_STREAMING_8_M:
441 sample_8bit = (uint8_t *)cur_buf->play_info->m_stream_data;
442 loop_end = sample_length = np_sample_length = cur_buf->play_info->m_stream_size;
443 break;
444 case SIF_STREAMING_16_S:
445 sample_16bit = (int16_t *)cur_buf->play_info->m_stream_data;
446 loop_end = sample_length = np_sample_length = cur_buf->play_info->m_stream_size / 4;
447 break;
448 case SIF_STREAMING_8_S:
449 sample_8bit = (uint8_t *)cur_buf->play_info->m_stream_data;
450 loop_end = sample_length = np_sample_length = cur_buf->play_info->m_stream_size / 2;
451 break;
452 default:
453 Int3();
454 break;
455 }
456 loop_end -= 1;
457 } else {
458 mprintf(0, "SE: Data is NULL\n");
459 cur_buf->m_status &= ~SSF_PLAY_STREAMING;
460 f_loop = false;
461 }
462 } else {
463 mprintf(0, "SE: Callback/data is NULL\n");
464 cur_buf->m_status &= ~SSF_PLAY_STREAMING;
465 f_loop = false;
466 }
467 }
468 }
469
470 if (f_loop)
471 goto looping;
472
473 done:
474
475 if (cur_buf->play_info->m_samples_played >= (np_sample_length) &&
476 !(cur_buf->m_status & (SSF_PLAY_LOOPING | SSF_PLAY_STREAMING))) {
477 m_ll_sound_ptr->StopSound(cur_buf->m_unique_id);
478 }
479 }
480
481 error_bail:
482 current_slot++;
483 }
484 }
485
486 inline void opti_8m_mix(uint8_t *cur_sample_8bit, const int num_write, int &samples_played, int16_t *mixer_buffer16,
487 const float l_volume, const float r_volume) {
488 int i;
489 int16_t *mb = mixer_buffer16;
490
491 for (i = 0; i < (num_write << 1); i += 2) {
492 int16_t sample;
493 int l_sample;
494 int r_sample;
495
496 sample = (((int16_t)(*cur_sample_8bit)) - (int16_t)128) << 8;
497 cur_sample_8bit++;
498
499 l_sample = *mb + (sample * l_volume);
500 r_sample = *(mb + 1) + (sample * r_volume);
501
502 samples_played++;
503
504 if (l_sample < -32767)
505 l_sample = -32767;
506 if (l_sample > 32767)
507 l_sample = 32767;
508 if (r_sample < -32767)
509 r_sample = -32767;
510 if (r_sample > 32767)
511 r_sample = 32767;
512
513 *mb = (int16_t)l_sample;
514 mb++;
515 *mb = (int16_t)r_sample;
516 mb++;
517 }
518 }
519
520 inline void opti_8s_mix(uint8_t *cur_sample_8bit, const int num_write, int &samples_played, int16_t *mixer_buffer16,
521 const float l_volume, const float r_volume) {
522 int i;
523 int16_t *mb = mixer_buffer16;
524
525 for (i = 0; i < (num_write << 1); i += 2) {
526 int16_t lsample;
527 int16_t rsample;
528 int l_sample;
529 int r_sample;
530
531 lsample = (((int16_t)(*cur_sample_8bit)) - (int16_t)128) << 8;
532 cur_sample_8bit++;
533 rsample = (((int16_t)(*cur_sample_8bit)) - (int16_t)128) << 8;
534 cur_sample_8bit++;
535
536 l_sample = *mb + (lsample * l_volume);
537 r_sample = *(mb + 1) + (rsample * r_volume);
538
539 samples_played++;
540
541 if (l_sample < -32767)
542 l_sample = -32767;
543 if (l_sample > 32767)
544 l_sample = 32767;
545 if (r_sample < -32767)
546 r_sample = -32767;
547 if (r_sample > 32767)
548 r_sample = 32767;
549
550 *mb = (int16_t)l_sample;
551 mb++;
552 *mb = (int16_t)r_sample;
553 mb++;
554 }
555 }
556
557 inline void opti_16m_mix(int16_t *cur_sample_16bit, const int num_write, int &samples_played, int16_t *mixer_buffer16,
558 const float l_volume, const float r_volume) {
559 int i;
560 int16_t *mb = mixer_buffer16;
561
562 for (i = 0; i < (num_write << 1); i += 2) {
563 int16_t sample;
564 int l_sample;
565 int r_sample;
566
567 sample = *cur_sample_16bit;
568 cur_sample_16bit++;
569
570 l_sample = *mb + (sample * l_volume);
571 r_sample = *(mb + 1) + (sample * r_volume);
572
573 samples_played++;
574
575 if (l_sample < -32767)
576 l_sample = -32767;
577 if (l_sample > 32767)
578 l_sample = 32767;
579 if (r_sample < -32767)
580 r_sample = -32767;
581 if (r_sample > 32767)
582 r_sample = 32767;
583
584 *mb = (int16_t)l_sample;
585 mb++;
586 *mb = (int16_t)r_sample;
587 mb++;
588 }
589 }
590
591 inline void opti_16s_mix(int16_t *cur_sample_16bit, const int num_write, int &samples_played, int16_t *mixer_buffer16,
592 const float l_volume, const float r_volume) {
593 int i;
594 int16_t *mb = mixer_buffer16;
595
596 for (i = 0; i < (num_write << 1); i += 2) {
597 int16_t lsample;
598 int16_t rsample;
599 int l_sample;
600 int r_sample;
601
602 lsample = *cur_sample_16bit;
603 cur_sample_16bit++;
604 rsample = *cur_sample_16bit;
605 cur_sample_16bit++;
606
607 l_sample = *mb + (lsample * l_volume);
608 r_sample = *(mb + 1) + (rsample * r_volume);
609
610 samples_played++;
611
612 if (l_sample < -32767)
613 l_sample = -32767;
614 if (l_sample > 32767)
615 l_sample = 32767;
616 if (r_sample < -32767)
617 r_sample = -32767;
618 if (r_sample > 32767)
619 r_sample = 32767;
620
621 *mb = (int16_t)l_sample;
622 mb++;
623 *mb = (int16_t)r_sample;
624 *mb++;
625 }
626 }