summaryrefslogtreecommitdiffhomepage
path: root/src/lingot-config.c
blob: a46e372f51e7f6e9e78ff0a09505e656c6817e6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//-*- C++ -*-
/*
 * lingot, a musical instrument tuner.
 *
 * Copyright (C) 2004-2007  Ibán Cereijo Graña, Jairo Chapela Martínez.
 *
 * This file is part of lingot.
 *
 * lingot is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * lingot is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with lingot; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

#include "lingot-config.h"
#include "lingot-mainframe.h"

// the following tokens will appear in the config file.
char* token[] =
  { "AUDIO_DEV", "SAMPLE_RATE", "OVERSAMPLING", "ROOT_FREQUENCY_ERROR",
      "MIN_FREQUENCY", "FFT_SIZE", "TEMPORAL_WINDOW", "NOISE_THRESHOLD",
      "CALCULATION_RATE", "VISUALIZATION_RATE", "PEAK_NUMBER", "PEAK_ORDER",
      "PEAK_REJECTION_RELATION", "DFT_NUMBER", "DFT_SIZE", "YIN_THRESHOLD",
      "ESTIMATOR", NULL // NULL terminated array
    };

// print/scan param formats.
const char* format = "sddffdffffddfddfd";

//----------------------------------------------------------------------------

LingotConfig* lingot_config_new()
  {

    LingotConfig* config = malloc(sizeof(LingotConfig));

    lingot_config_reset(config); // set default values.

    // internal parameters mapped to each token in the config file.
    void* c_param[] =
      { &config->audio_dev, &config->sample_rate, &config->oversampling,
          &config->root_frequency_error, &config->min_frequency,
          &config->fft_size, &config->temporal_window,
          &config->noise_threshold_db, &config->calculation_rate,
          &config->visualization_rate, &config->peak_number,
          &config->peak_order, &config->peak_rejection_relation_db,
          &config->dft_number, &config->dft_size, &config->yin_threshold,
          &config->estimator };

    memcpy(config->param, c_param, 17*sizeof(void*));

    return config;
  }

void lingot_config_destroy(LingotConfig* config)
  {
    free(config);
  }

//----------------------------------------------------------------------------
void lingot_config_reset(LingotConfig* config)
  {
    sprintf(config->audio_dev, "%s", "/dev/dsp");

    config->sample_rate = 44100; // Hz
    config->oversampling = 5;
    config->root_frequency_error = 0; // Hz
    config->min_frequency = 15; // Hz
    config->fft_size = 512; // samples
    config->temporal_window = 0.32; // seconds
    config->calculation_rate = 20; // Hz
    config->visualization_rate = 30; // Hz
    config->noise_threshold_db = 20.0; // dB

    config->peak_number = 3; // peaks
    config->peak_order = 1; // samples
    config->peak_rejection_relation_db = 20; // dB

    config->dft_number = 2; // DFTs
    config->dft_size = 15; // samples

    config->max_nr_iter = 10; // iterations

    //--------------------------------------------------------------------------

    config->vr = -0.45; // near to minimum

    config->yin_threshold = 0.1;
    config->estimator = 0;

    lingot_config_update_internal_params(config);
  }

//----------------------------------------------------------------------------

int lingot_config_update_internal_params(LingotConfig* config)
  {
    int result = 1;

    // derived parameters.
    config->root_frequency = 440.0
        *pow(2.0, config->root_frequency_error/1200.0);
    config->temporal_buffer_size = (unsigned int)ceil(config->temporal_window
        *config->sample_rate/config->oversampling);
    config->read_buffer_size = (unsigned int)ceil(config->sample_rate
        /(config->calculation_rate*config->oversampling));
    config->peak_rejection_relation_nu = pow(10.0,
        config->peak_rejection_relation_db/10.0);
    config->noise_threshold_nu = pow(10.0, config->noise_threshold_db/10.0);

    if (config->temporal_buffer_size < config->fft_size)
      {
        config->temporal_window = ((double) config->fft_size
            *config->oversampling)/config->sample_rate;
        config->temporal_buffer_size = config->fft_size;
        result = 0;
      }

    return result;
  }

//----------------------------------------------------------------------------

void lingot_config_save(LingotConfig* config, char* filename)
  {
    unsigned int i;
    FILE* fp;

    if ((fp = fopen(filename, "w")) == NULL)
      {
        char buff[100];
        sprintf(buff, "error saving config file %s ", filename);
        perror(buff);
        return;
      }

    fprintf(fp, "# Config file automatically created by lingot\n\n");

    for (i = 0; token[i]; i++)
      switch (format[i])
        {
        case 's':
          fprintf(fp, "%s = %s\n", token[i], (char*) config->param[i]);
          break;
        case 'd':
          fprintf(fp, "%s = %d\n", token[i],
              *((unsigned int*) config->param[i]));
          break;
        case 'f':
          fprintf(fp, "%s = %0.3f\n", token[i], *((FLT*) config->param[i]));
          break;
        }

    fclose(fp);
  }

//----------------------------------------------------------------------------

void lingot_config_load(LingotConfig* config, char* filename)
  {
    FILE* fp;
    float aux;
    int line;
    int token_index;
    char* char_buffer_pointer;

    const static char* delim = " \t=\n";
    
#   define MAX_LINE_SIZE 100

    char char_buffer[MAX_LINE_SIZE];

    if ((fp = fopen(filename, "r")) == NULL)
      {
        sprintf(char_buffer,
            "error opening config file %s, assuming default values ", filename);
        perror(char_buffer);
        return;
      }

    line = 0;

    for (;;)
      {

        line++;

        if (!fgets(char_buffer, MAX_LINE_SIZE, fp))
        break;;

        //    printf("line %d: %s\n", line, s1);

        if (char_buffer[0] == '#')
        continue;

        // tokens into the line.
        char_buffer_pointer = strtok(char_buffer, delim);

        if (!char_buffer_pointer)
        continue; // blank line.

        for (token_index = 0; token[token_index]; token_index++)
          {
            if (!strcmp(char_buffer_pointer, token[token_index]))
              {
                break; // found token.
              }
          }

        if (!token[token_index])
          {
            fprintf(stderr,
                "warning: parse error at line %i: unknown keyword %s\n",
                line, char_buffer_pointer);
            continue;
          }

        // take the attribute value.
        char_buffer_pointer = strtok(NULL, delim);

        if (!char_buffer_pointer)
          {
            fprintf(stderr, "warning: parse error at line %i: value expected\n",
                line);
            continue;
          }

        // asign the value to the parameter.
        switch (format[token_index])
          {
            case 's':
            sprintf(((char*) config->param[token_index]), "%s",
                char_buffer_pointer);
            break;
            case 'd':
            sscanf(char_buffer_pointer, "%d",
                (unsigned int*) config->param[token_index]);
            break;
            case 'f':
            sscanf(char_buffer_pointer, "%f", &aux);
            *((FLT*) config->param[token_index]) = aux;
            break;
          }

      }

    fclose(fp);

    lingot_config_update_internal_params(config);

#   undef MAX_LINE_SIZE
  }