annotate src/audacious/src_flow.c @ 4747:0cc36efdbe07

Don't use a static buffer, but g_strdup_vprintf().
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 30 Jul 2008 17:15:13 +0300
parents b3e4f5c31546
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4259
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
1 /* Audacious - Cross-platform multimedia player
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
2 * Copyright (C) 2005-2008 Audacious development team
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
3 *
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; under version 3 of the License.
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
7 *
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
12 *
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
13 * You should have received a copy of the GNU General Public License
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
14 * along with this program. If not, see <http://www.gnu.org/licenses>.
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
15 *
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
16 * The Audacious team does not consider modular code linking to
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
17 * Audacious or using our public API to be a derived work.
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
18 */
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
19
4269
2a4954d14bfb removed debugging stuff...
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4267
diff changeset
20 /*#define AUD_DEBUG*/
4267
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
21
4259
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
22 #ifdef HAVE_CONFIG_H
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
23 # include "config.h"
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
24 #endif
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
25
4523
b3e4f5c31546 Conditional USE_SRC was changed to USE_SAMPLERATE earlier.
Matti Hamalainen <ccr@tnsp.org>
parents: 4290
diff changeset
26 #ifdef USE_SAMPLERATE
4259
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
27
4267
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
28 #include <stdlib.h>
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
29 #include <stdio.h>
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
30 #include <math.h>
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
31
4259
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
32 #include <samplerate.h>
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
33
4267
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
34 #include "main.h"
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
35 #include "plugin.h"
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
36 #include "input.h"
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
37 #include "playback.h"
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
38 #include "util.h"
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
39
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
40 #include "src_flow.h"
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
41
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
42 static SRC_STATE *src_state = NULL;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
43 static SRC_DATA src_data;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
44
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
45 static int overSamplingFs = 48000; /* hmmm... does everybody have 96kHz-enabled hardware? i'm in doubt --asphyx */
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
46 static int inputFs = 0;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
47 static int input_nch = 0;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
48 static int converter_type = SRC_SINC_BEST_QUALITY;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
49 static int srcError = 0;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
50
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
51 static float *srcOut = NULL;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
52 static unsigned int lengthOfSrcOut = 0;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
53
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
54 void
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
55 src_flow_free()
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
56 {
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
57 AUDDBG("\n");
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
58 if(src_state != NULL) src_state = src_delete(src_state);
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
59 src_state = NULL;
4290
5b277773870e don't change src settings in play time
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4269
diff changeset
60 /*if(srcOut != NULL) free(srcOut);
4267
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
61 srcOut = NULL;
4290
5b277773870e don't change src settings in play time
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4269
diff changeset
62 lengthOfSrcOut = 0;*/
4267
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
63 inputFs = 0;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
64 overSamplingFs = 0;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
65 input_nch = 0;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
66 }
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
67
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
68 gint
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
69 src_flow_init(gint infreq, gint nch)
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
70 {
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
71 src_flow_free();
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
72
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
73 /* don't resample if sampling rates are the same --nenolod */
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
74 if (infreq == cfg.src_rate || !cfg.enable_src) return infreq;
4290
5b277773870e don't change src settings in play time
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4269
diff changeset
75 AUDDBG("input_rate=%d, output_rate=%d, nch=%d\n", infreq, cfg.src_rate, nch);
4267
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
76
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
77 overSamplingFs = cfg.src_rate;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
78 inputFs = infreq;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
79 input_nch = nch;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
80 converter_type = cfg.src_type;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
81
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
82 src_state = src_new(converter_type, nch, &srcError);
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
83 if (src_state != NULL) {
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
84 src_data.src_ratio = (float)overSamplingFs / (float)infreq;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
85 return overSamplingFs;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
86 } else {
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
87 AUDDBG("src_new(): %s\n\n", src_strerror(srcError));
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
88 return infreq;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
89 }
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
90 }
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
91
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
92 void
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
93 src_flow(FlowContext *context) {
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
94
4290
5b277773870e don't change src settings in play time
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4269
diff changeset
95 if(src_state == NULL) return;
4267
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
96
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
97 if(context->fmt != FMT_FLOAT) {
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
98 context->error = TRUE;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
99 return;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
100 }
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
101
4290
5b277773870e don't change src settings in play time
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4269
diff changeset
102 if(context->srate != inputFs || context->channels != input_nch) {
4267
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
103 AUDDBG("reinitializing src\n");
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
104 src_flow_init(context->srate, context->channels);
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
105 }
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
106
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
107 int lrLength = context->len;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
108 int overLrLength = (int)floor(lrLength * (src_data.src_ratio + 1));
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
109
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
110 if(lengthOfSrcOut < overLrLength || srcOut == NULL) {
4290
5b277773870e don't change src settings in play time
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4269
diff changeset
111 AUDDBG("reallocating srcOut (%d bytes)\n", overLrLength);
4267
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
112 lengthOfSrcOut = overLrLength;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
113 srcOut = smart_realloc(srcOut, &lengthOfSrcOut);
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
114 }
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
115
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
116 src_data.data_in = (float*)context->data;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
117 src_data.data_out = srcOut;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
118 src_data.end_of_input = 0;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
119 src_data.input_frames = lrLength / context->channels / sizeof(float);
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
120 src_data.output_frames = overLrLength / context->channels / sizeof(float);
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
121 if ((srcError = src_process(src_state, &src_data)) > 0) {
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
122 AUDDBG("src_process(): %s\n", src_strerror(srcError));
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
123 context->error = TRUE;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
124 return;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
125 } else {
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
126 context->data = (gpointer) srcOut;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
127 context->len = src_data.output_frames_gen * context->channels * sizeof(float);
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
128 return;
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
129 }
a41fb6bc632a - src stuff traveled to src_flow.c
Eugene Zagidullin <e.asphyx@gmail.com>
parents: 4259
diff changeset
130 }
4259
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
131
4523
b3e4f5c31546 Conditional USE_SRC was changed to USE_SAMPLERATE earlier.
Matti Hamalainen <ccr@tnsp.org>
parents: 4290
diff changeset
132 #endif /* USE_SAMPLERATE */
4259
92642f860860 - added template for src_flow
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
diff changeset
133