~mixxxdevelopers/mixxx/mixxx-buildserver

« back to all changes in this revision

Viewing changes to mixxx/lib/libsamplerate/src_linear.c

  • Committer: Albert Santoni
  • Date: 2011-03-20 00:27:15 UTC
  • mfrom: (2607.1.162 mixxx-1.9)
  • Revision ID: alberts@mixxx.org-20110320002715-sa2d88zbuc5kkyya
MergedĀ fromĀ 1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
** Copyright (C) 2002,2003 Erik de Castro Lopo <erikd@mega-nerd.com>
3
 
**
4
 
** This program is free software; you can redistribute it and/or modify
5
 
** it under the terms of the GNU General Public License as published by
6
 
** the Free Software Foundation; either version 2 of the License, or
7
 
** (at your option) any later version.
8
 
**
9
 
** This program is distributed in the hope that it will be useful,
10
 
** but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
** GNU General Public License for more details.
13
 
**
14
 
** You should have received a copy of the GNU General Public License
15
 
** along with this program; if not, write to the Free Software
16
 
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
17
 
*/
18
 
 
19
 
#include <stdio.h>
20
 
#include <stdlib.h>
21
 
#include <string.h>
22
 
 
23
 
#include "config.h"
24
 
#include "float_cast.h"
25
 
#include "common.h"
26
 
 
27
 
static void linear_reset (SRC_PRIVATE *psrc) ;
28
 
 
29
 
/*========================================================================================
30
 
*/
31
 
 
32
 
#define LINEAR_MAGIC_MARKER     MAKE_MAGIC('l','i','n','e','a','r')
33
 
 
34
 
typedef struct
35
 
{       int             linear_magic_marker ;
36
 
        int             channels ;
37
 
        long    in_count, in_used ;
38
 
        long    out_count, out_gen ;
39
 
        float   last_value [1] ;
40
 
} LINEAR_DATA ;
41
 
 
42
 
/*----------------------------------------------------------------------------------------
43
 
*/
44
 
int
45
 
linear_process (SRC_PRIVATE *psrc, SRC_DATA *data)
46
 
{       LINEAR_DATA *linear ;
47
 
        double          src_ratio, input_index ;
48
 
        int                     ch ;
49
 
 
50
 
        if (psrc->private_data == NULL)
51
 
                return SRC_ERR_NO_PRIVATE ;
52
 
 
53
 
        linear = (LINEAR_DATA*) psrc->private_data ;
54
 
 
55
 
        linear->in_count = data->input_frames * linear->channels ;
56
 
        linear->out_count = data->output_frames * linear->channels ;
57
 
        linear->in_used = linear->out_gen = 0 ;
58
 
 
59
 
        src_ratio = psrc->last_ratio ;
60
 
        input_index = psrc->last_position ;
61
 
 
62
 
        /* Calculate samples before first sample in input array. */
63
 
        while (input_index > 0.0 && input_index < 1.0 && linear->out_gen < linear->out_count)
64
 
        {
65
 
                if (linear->in_used + input_index > linear->in_count)
66
 
                        break ;
67
 
 
68
 
                if (fabs (psrc->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
69
 
                        src_ratio = psrc->last_ratio + linear->out_gen * (data->src_ratio - psrc->last_ratio) / (linear->out_count - 1) ;
70
 
 
71
 
                for (ch = 0 ; ch < linear->channels ; ch++)
72
 
                {       data->data_out [linear->out_gen] = linear->last_value [ch] + input_index *
73
 
                                                                                (data->data_in [ch] - linear->last_value [ch]) ;
74
 
                        linear->out_gen ++ ;
75
 
                        } ;
76
 
 
77
 
                /* Figure out the next index. */
78
 
                input_index += 1.0 / src_ratio ;
79
 
                } ;
80
 
 
81
 
        /* Main processing loop. */
82
 
        while (linear->out_gen < linear->out_count)
83
 
        {
84
 
                linear->in_used += linear->channels * lrint (floor (input_index)) ;
85
 
                input_index -= floor (input_index) ;
86
 
 
87
 
                if (linear->in_used + input_index > linear->in_count)
88
 
                        break ;
89
 
 
90
 
                if (fabs (psrc->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
91
 
                        src_ratio = psrc->last_ratio + linear->out_gen * (data->src_ratio - psrc->last_ratio) / (linear->out_count - 1) ;
92
 
 
93
 
                for (ch = 0 ; ch < linear->channels ; ch++)
94
 
                {       data->data_out [linear->out_gen] = data->data_in [linear->in_used + ch] + input_index *
95
 
                                                (data->data_in [linear->in_used + linear->channels + ch] - data->data_in [linear->in_used + ch]) ;
96
 
                        linear->out_gen ++ ;
97
 
                        } ;
98
 
 
99
 
                /* Figure out the next index. */
100
 
                input_index += 1.0 / src_ratio ;
101
 
                } ;
102
 
 
103
 
/*-     if (input_index > linear->in_count - linear->in_used)
104
 
        {       input_index -= linear->in_count - linear->in_used ;
105
 
                linear->in_used = linear->in_count ;
106
 
                puts ("XXXXXXXXXX") ; /+*-exit (1) ;-*+/
107
 
                } ;     
108
 
-*/
109
 
 
110
 
        psrc->last_position = input_index ;
111
 
 
112
 
        for (ch = 0 ; ch < linear->channels ; ch++)
113
 
        {       linear->last_value [ch] = data->data_in [linear->in_used - linear->channels + ch] ;
114
 
 
115
 
/*-             data->data_out [0 + ch] = -0.9 ;
116
 
                data->data_out [linear->out_gen - linear->channels + ch] = 0.9 ; -*/
117
 
                } ;
118
 
 
119
 
        /* Save current ratio rather then target ratio. */
120
 
        psrc->last_ratio = src_ratio ;
121
 
 
122
 
        data->input_frames_used = linear->in_used / linear->channels ;
123
 
        data->output_frames_gen = linear->out_gen / linear->channels ;
124
 
 
125
 
        return SRC_ERR_NO_ERROR ;
126
 
} /* linear_process */
127
 
 
128
 
/*------------------------------------------------------------------------------
129
 
*/
130
 
 
131
 
const char*
132
 
linear_get_name (int src_enum)
133
 
{
134
 
        if (src_enum == SRC_LINEAR)
135
 
                return "Linear Interpolator" ;
136
 
 
137
 
        return NULL ;
138
 
} /* linear_get_name */
139
 
 
140
 
const char*
141
 
linear_get_description (int src_enum)
142
 
{
143
 
        if (src_enum == SRC_LINEAR)
144
 
                return "Linear interpolator, very fast, poor quality." ;
145
 
 
146
 
        return NULL ;
147
 
} /* linear_get_descrition */
148
 
 
149
 
int
150
 
linear_set_converter (SRC_PRIVATE *psrc, int src_enum)
151
 
{       LINEAR_DATA *linear ;
152
 
 
153
 
        if (src_enum != SRC_LINEAR)
154
 
                return SRC_ERR_BAD_CONVERTER ;
155
 
 
156
 
        if (psrc->private_data != NULL)
157
 
        {       linear = (LINEAR_DATA*) psrc->private_data ;
158
 
                if (linear->linear_magic_marker != LINEAR_MAGIC_MARKER)
159
 
                {       free (psrc->private_data) ;
160
 
                        psrc->private_data = NULL ;
161
 
                        } ;
162
 
                } ;
163
 
 
164
 
        if (psrc->private_data == NULL)
165
 
        {       linear = calloc (1, sizeof (*linear) + psrc->channels * sizeof (float)) ;
166
 
                if (linear == NULL)
167
 
                        return SRC_ERR_MALLOC_FAILED ;
168
 
                psrc->private_data = linear ;
169
 
                } ;
170
 
 
171
 
        linear->linear_magic_marker = LINEAR_MAGIC_MARKER ;
172
 
        linear->channels = psrc->channels ;
173
 
 
174
 
        psrc->process = linear_process ;
175
 
        psrc->reset = linear_reset ;
176
 
 
177
 
        linear_reset (psrc) ;
178
 
 
179
 
        return SRC_ERR_NO_ERROR ;
180
 
} /* linear_set_converter */
181
 
 
182
 
/*===================================================================================
183
 
*/
184
 
 
185
 
static void
186
 
linear_reset (SRC_PRIVATE *psrc)
187
 
{       LINEAR_DATA *linear ;
188
 
 
189
 
        linear = (LINEAR_DATA*) psrc->private_data ;
190
 
        if (linear == NULL)
191
 
                return ;
192
 
 
193
 
        memset (linear->last_value, 0, sizeof (linear->last_value [0]) * linear->channels) ;
194
 
} /* linear_reset */