annotate mixer/mixercore.c @ 576:8d62447b75f2

Added new Core Audio backend.
author Eric Wing <ewing . public |-at-| gmail . com>
date Sun, 10 Oct 2010 21:30:17 -0700
parents 2e8907ff98e9
children
rev   line source
486
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
1 /*
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
2 * SDL_sound -- An sound processing toolkit.
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
3 * Copyright (C) 2001 Ryan C. Gordon.
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
4 *
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
8 * version 2.1 of the License, or (at your option) any later version.
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
9 *
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
13 * Lesser General Public License for more details.
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
14 *
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
18 */
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
19
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
20 /**
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
21 * This file implements the mixer itself. Largely, this is handled in the
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
22 * SDL audio callback.
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
23 *
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
24 * Documentation is in SDL_sound.h ... It's verbose, honest. :)
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
25 *
552
2e8907ff98e9 Replaced references to COPYING with references to LICENSE.txt ...
Ryan C. Gordon <icculus@icculus.org>
parents: 526
diff changeset
26 * Please see the file LICENSE.txt in the source's root directory.
486
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
27 *
526
2df1f5c62d38 Updated my email address.
Ryan C. Gordon <icculus@icculus.org>
parents: 486
diff changeset
28 * This file written by Ryan C. Gordon. (icculus@icculus.org)
486
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
29 */
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
30
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
31 #if HAVE_CONFIG_H
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
32 # include <config.h>
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
33 #endif
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
34
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
35 #include <stdio.h>
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
36 #include <stdlib.h>
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
37 #include <string.h>
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
38
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
39 #include "SDL.h"
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
40 #include "SDL_thread.h"
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
41 #include "SDL_sound.h"
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
42
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
43 #define __SDL_SOUND_INTERNAL__
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
44 #include "SDL_sound_internal.h"
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
45
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
46
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
47
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
48 typedef struct S_PlayingList
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
49 {
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
50 Sound_Sample *sample;
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
51 struct S_PlayingList *next;
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
52 } PlayingList;
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
53
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
54 static PlayingList *playlist = NULL;
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
55
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
56 static inline void mix_predecoded(Sound_Sample *samp,
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
57 UInt32 *samp_frames_left,
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
58 float *gains)
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
59 {
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
60 Sound_SampleInternal *internal = (Sound_SampleInternal *) samp->opaque;
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
61 Uint32 sfl = *samp_frames_left; /* move to a local. */
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
62 Uint32 max = internal->buffer_size - internal->mix_position;
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
63 float *wptr; /* write pointer */
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
64
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
65 /* !!! FIXME: max must be converted to sample frame count... */
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
66
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
67 if (max > sfl) /* we have more data than mix buffer? */
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
68 max = sfl;
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
69
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
70 assert(max > 0);
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
71 *samp_frames_left -= max;
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
72
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
73 wptr = mixbuf + ((mixbufsize / sizeof (float)) - (max * MAX_CHANNELS));
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
74 internal->mix(wptr, internal->buffer, max, gains);
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
75 } /* mix_predecoded */
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
76
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
77
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
78 static void mix_playing_samples(Uint8 *stream, int len)
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
79 {
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
80 PlayingList *samples = playlist;
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
81 const int frames = len / framesize;
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
82 const Uint32 ticks = SDL_GetTicks(); /* used for calculating fade. */
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
83
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
84 while (samples) /* iterate linked list of playing samples... */
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
85 {
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
86 Sound_Sample *samp = samples->sample;
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
87 Uint32 sample_frames_left = mixbuf_frames;
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
88 float gains[MAX_CHANNELS];
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
89
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
90 calculate_gains(samp, ticks, gains);
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
91 while (sample_frames_left)
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
92 {
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
93 mix_predecoded(samp, &sample_frames_left);
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
94 if (!decode_more(samp))
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
95 break;
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
96 } /* while */
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
97
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
98 samples = samples->next; /* set up for next iteration. */
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
99 } /* while */
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
100 } /* mix_playing_samples */
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
101
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
102
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
103 static inline void run_pre_mix(void)
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
104 {
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
105 if (premixer)
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
106 premixer(mixbuf, mixbufsize);
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
107 else /* !!! FIXME: Do memset in another thread after mix is done. */
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
108 memset(mixbuf, '\0', mixbufsize * sizeof (float) * 2);
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
109 } /* run_pre_mix */
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
110
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
111
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
112 static inline void run_post_mix(void)
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
113 {
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
114 if (postmixer)
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
115 postmixer(mixbuf, mixbufsize);
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
116 } /* run_post_mix */
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
117
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
118
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
119 /* this is where it happens: the SDL audio callback. */
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
120 static void audio_callback(void *userdata, Uint8 *stream, int len)
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
121 {
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
122 mixer_callback_running = 1;
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
123 run_pre_mix();
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
124 mix_playing_samples();
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
125 run_post_mix();
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
126 mixer_callback_running = 0;
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
127 } /* audio_callback */
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
128
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
129 /* end of mixercore.c ... */
859dd2ef3197 Added some seriously INCOMPLETE mixer code.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
130