File: osc-gen-file.cpp

package info (click to toggle)
ecasound 2.9.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 6,292 kB
  • sloc: cpp: 39,475; sh: 4,335; lisp: 1,918; ansic: 1,883; makefile: 888; python: 617; ruby: 202
file content (131 lines) | stat: -rw-r--r-- 3,751 bytes parent folder | download | duplicates (5)
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
// ------------------------------------------------------------------------
// osc-gen-file.cpp: Generic oscillator using envelope presets
// Copyright (C) 1999-2002,2004,2007,2012 Kai Vehmanen
//
// Attributes:
//     eca-style-version: 3
//
// This program is fre 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.
// 
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
// ------------------------------------------------------------------------

#include <cstdlib>
#include <vector>
#include <string>

#include <kvu_numtostr.h>
#include <kvu_utils.h>

#include "resource-file.h"
#include "eca-resources.h"
#include "osc-gen-file.h"
#include "eca-logger.h"

GENERIC_OSCILLATOR_FILE::GENERIC_OSCILLATOR_FILE(double freq, int mode)
  : GENERIC_OSCILLATOR(freq, mode)
{
  set_parameter(1, get_parameter(1));
  set_parameter(2, mode);
}

GENERIC_OSCILLATOR_FILE::~GENERIC_OSCILLATOR_FILE (void)
{
}

void GENERIC_OSCILLATOR_FILE::get_oscillator_preset(int preset)
{
  ECA_RESOURCES ecarc;
  ECA_LOG_MSG(ECA_LOGGER::system_objects, "Opening genosc envelope file.");

  std::string user_filename =
    ecarc.resource("user-resource-directory") + "/" + ecarc.resource("resource-file-genosc-envelopes");

  std::string pname = kvu_numtostr(preset);

  RESOURCE_FILE rc;
  rc.resource_file(user_filename);
  rc.load();
  if (rc.has(pname) != true) {
    std::string global_filename =
      ecarc.resource("resource-directory") + "/" + ecarc.resource("resource-file-genosc-envelopes");
    rc.resource_file(global_filename);
    rc.load();
  }

  if (rc.has(pname) == true) {
    parse_envelope(rc.resource(pname));
  }
  else {
    ECA_LOG_MSG(ECA_LOGGER::info, "ERROR: Oscillator preset " + pname + " not found!");
  }
}

void GENERIC_OSCILLATOR_FILE::parse_envelope(const std::string& str)
{
  std::vector<std::string> tokens = kvu_string_to_words(str);
  size_t dynparams_offset = 2;

  // number of points
  if (tokens.size() > 2) {
    GENERIC_OSCILLATOR::set_parameter(3,
      (tokens.size() - dynparams_offset) / 2);
  }
  else {
    GENERIC_OSCILLATOR::set_parameter(3, 0);
  }

  if (tokens.size() > 0) {
    // start value
    GENERIC_OSCILLATOR::set_parameter(4, atof(tokens[0].c_str()));
    if (tokens.size() > 1) {
      // start value
      GENERIC_OSCILLATOR::set_parameter(5, atof(tokens[1].c_str()));

      // set individual points
      for(unsigned int n = dynparams_offset; n < tokens.size(); n++) {
        GENERIC_OSCILLATOR::set_parameter(6 + n - dynparams_offset,
                                          atof(tokens[n].c_str()));
      }
    }
  }
  prepare_envelope();
}

void GENERIC_OSCILLATOR_FILE::set_parameter(int param, CONTROLLER_SOURCE::parameter_t value)
{
  switch (param) {
  case 1: 
  case 2: 
    GENERIC_OSCILLATOR::set_parameter(param, value);
    break;

  case 3:
    preset_rep = static_cast<int>(value);
    get_oscillator_preset(preset_rep);
    break;
  }
}

CONTROLLER_SOURCE::parameter_t GENERIC_OSCILLATOR_FILE::get_parameter(int param) const
{
  switch (param) {
  case 1: 
  case 2:
    return GENERIC_OSCILLATOR::get_parameter(param);

  case 3:
    return static_cast<parameter_t>(preset_rep);
  }
  return 0.0;
}