~ubuntu-branches/ubuntu/wily/ecasound/wily-proposed

« back to all changes in this revision

Viewing changes to libecasound/audioio-acseq.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghedini
  • Date: 2011-05-12 17:58:03 UTC
  • Revision ID: james.westby@ubuntu.com-20110512175803-zy3lodjecabt9r3v
Tags: upstream-2.8.0
ImportĀ upstreamĀ versionĀ 2.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ------------------------------------------------------------------------
 
2
// audioio-audioseq.cpp: Audio clip sequencer class.
 
3
// Copyright (C) 2008,2010 Kai Vehmanen
 
4
//
 
5
// Attributes:
 
6
//     eca-style-version: 3 (see Ecasound Programmer's Guide)
 
7
//
 
8
// This program is free software; you can redistribute it and/or modify
 
9
// it under the terms of the GNU General Public License as published by
 
10
// the Free Software Foundation; either version 2 of the License, or
 
11
// (at your option) any later version.
 
12
// 
 
13
// This program is distributed in the hope that it will be useful,
 
14
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
// GNU General Public License for more details.
 
17
// 
 
18
// You should have received a copy of the GNU General Public License
 
19
// along with this program; if not, write to the Free Software
 
20
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 
21
// ------------------------------------------------------------------------
 
22
 
 
23
#include <algorithm>
 
24
#include <string>
 
25
#include <vector>
 
26
#include <iostream>
 
27
#include <fstream>
 
28
#include <cmath>
 
29
 
 
30
#include <kvu_message_item.h>
 
31
#include <kvu_numtostr.h>
 
32
#include <kvu_dbc.h>
 
33
 
 
34
#include "eca-object-factory.h"
 
35
#include "samplebuffer.h"
 
36
#include "audioio-acseq.h"
 
37
 
 
38
#include "eca-error.h"
 
39
#include "eca-logger.h"
 
40
 
 
41
using std::cout;
 
42
using std::endl;
 
43
using SAMPLE_SPECS::sample_pos_t;
 
44
 
 
45
/**
 
46
 * FIXME notes  (last update 2008-03-04)
 
47
 *
 
48
 * - None.
 
49
 */
 
50
 
 
51
AUDIO_CLIP_SEQUENCER::AUDIO_CLIP_SEQUENCER ()
 
52
{
 
53
  set_label("audiocseq");
 
54
 
 
55
  /* note: index of last sequencer parameter (one if no
 
56
   *       extra parameters); params beyond this value are
 
57
   *       passed on to the child object */
 
58
  child_param_offset_rep = 1;
 
59
 
 
60
  cseq_mode_rep = AUDIO_CLIP_SEQUENCER::cseq_none;
 
61
}
 
62
 
 
63
AUDIO_CLIP_SEQUENCER::~AUDIO_CLIP_SEQUENCER(void)
 
64
{
 
65
}
 
66
 
 
67
AUDIO_CLIP_SEQUENCER* AUDIO_CLIP_SEQUENCER::clone(void) const
 
68
{
 
69
  AUDIO_CLIP_SEQUENCER* target = new AUDIO_CLIP_SEQUENCER();
 
70
  for(int n = 0; n < number_of_params(); n++) {
 
71
    target->set_parameter(n + 1, get_parameter(n + 1));
 
72
  }
 
73
  return target;
 
74
}
 
75
 
 
76
 
 
77
void AUDIO_CLIP_SEQUENCER::open(void) throw(AUDIO_IO::SETUP_ERROR &)
 
78
{
 
79
  if (io_mode() != AUDIO_IO::io_read)
 
80
    throw(SETUP_ERROR(SETUP_ERROR::unexpected, "AUDIOIO-ACLIPSEQ: Only read mode supported."));
 
81
 
 
82
  ECA_LOG_MSG(ECA_LOGGER::user_objects, 
 
83
              "Opening audio clip sequencer in mode: " 
 
84
              + get_parameter(1));
 
85
 
 
86
  /* note: change behaviour based on first param */
 
87
  if (cseq_mode_rep == AUDIO_CLIP_SEQUENCER::cseq_loop) {
 
88
    /* following is specific to looping */
 
89
    AUDIO_SEQUENCER_BASE::toggle_looping(true);
 
90
    DBC_CHECK(finite_length_stream() != true);
 
91
    AUDIO_SEQUENCER_BASE::set_child_object_string(
 
92
      child_params_as_string(1 + child_param_offset_rep, &params_rep));
 
93
  }
 
94
  else if (cseq_mode_rep == AUDIO_CLIP_SEQUENCER::cseq_select) {
 
95
    AUDIO_SEQUENCER_BASE::toggle_looping(false);
 
96
    AUDIO_SEQUENCER_BASE::set_child_start_position(get_parameter(2));
 
97
    AUDIO_SEQUENCER_BASE::set_child_length(get_parameter(3));
 
98
    AUDIO_SEQUENCER_BASE::set_child_object_string(
 
99
      child_params_as_string(1 + child_param_offset_rep, &params_rep));
 
100
  }
 
101
  else if (cseq_mode_rep == AUDIO_CLIP_SEQUENCER::cseq_play_at) {
 
102
    /* following is specific to play-at */
 
103
    AUDIO_SEQUENCER_BASE::toggle_looping(false);
 
104
    AUDIO_SEQUENCER_BASE::set_child_offset(get_parameter(2));
 
105
    AUDIO_SEQUENCER_BASE::set_child_object_string(
 
106
      child_params_as_string(1 + child_param_offset_rep, &params_rep));
 
107
  }
 
108
  else
 
109
    throw(SETUP_ERROR(SETUP_ERROR::unexpected, "AUDIOIO-ACLIPSEQ: Unknown audio sequencing mode (loop, select, ...)."));
 
110
 
 
111
  AUDIO_SEQUENCER_BASE::open();
 
112
 
 
113
  /* step: set additional child params (if any) */
 
114
  int numparams = child()->number_of_params();
 
115
  for(int n = 0; n < numparams; n++) {
 
116
    child()->set_parameter(n + 1, get_parameter(n + 1 + child_param_offset_rep));
 
117
    if (child()->variable_params())
 
118
      numparams = child()->number_of_params();
 
119
  }
 
120
}
 
121
 
 
122
void AUDIO_CLIP_SEQUENCER::close(void)
 
123
{
 
124
  AUDIO_SEQUENCER_BASE::close();
 
125
}
 
126
 
 
127
std::string AUDIO_CLIP_SEQUENCER::parameter_names(void) const
 
128
{
 
129
  std::string baseparams;
 
130
 
 
131
  if (cseq_mode_rep == AUDIO_CLIP_SEQUENCER::cseq_loop)
 
132
    baseparams += std::string("audioloop");
 
133
  else if (cseq_mode_rep == AUDIO_CLIP_SEQUENCER::cseq_select)
 
134
    baseparams += std::string("select,start-sec,dur-sec");
 
135
  else if (cseq_mode_rep == AUDIO_CLIP_SEQUENCER::cseq_play_at)
 
136
    baseparams += std::string("playat,pos-sec");
 
137
  else
 
138
    baseparams += std::string("acseqtype");
 
139
 
 
140
  if (is_child_initialized() == true) {
 
141
    baseparams += "," + child()->parameter_names();
 
142
  }
 
143
  else {
 
144
    /* create a generic parameter name list */
 
145
    for (size_t i = 1; i < params_rep.size(); i++) {
 
146
      baseparams += ",param" + kvu_numtostr(i);
 
147
    }
 
148
  }
 
149
    
 
150
  ECA_LOG_MSG(ECA_LOGGER::system_objects, 
 
151
              "param list: " + baseparams);
 
152
 
 
153
  return baseparams;
 
154
}
 
155
 
 
156
void AUDIO_CLIP_SEQUENCER::set_parameter(int param, string value)
 
157
{
 
158
  ECA_LOG_MSG(ECA_LOGGER::user_objects, 
 
159
              AUDIO_IO::parameter_set_to_string(param, value));
 
160
 
 
161
  if (param > static_cast<int>(params_rep.size())) params_rep.resize(param);
 
162
 
 
163
  if (param > 0)
 
164
    params_rep[param - 1] = value;
 
165
 
 
166
  if (param == 1) {
 
167
    set_label(value);
 
168
    if (value == "audioloop") {
 
169
      cseq_mode_rep = AUDIO_CLIP_SEQUENCER::cseq_loop;
 
170
      child_param_offset_rep = 1;
 
171
    }
 
172
    else if (value == "select") {
 
173
      cseq_mode_rep = AUDIO_CLIP_SEQUENCER::cseq_select;
 
174
      child_param_offset_rep = 3;
 
175
    }
 
176
    else if (value == "playat") {
 
177
      cseq_mode_rep = AUDIO_CLIP_SEQUENCER::cseq_play_at;
 
178
      child_param_offset_rep = 2;
 
179
    }
 
180
    else {
 
181
      cseq_mode_rep = AUDIO_CLIP_SEQUENCER::cseq_none;
 
182
      child_param_offset_rep = 1;
 
183
    }
 
184
  }
 
185
 
 
186
  if (param > child_param_offset_rep && 
 
187
      is_child_initialized() == true) {
 
188
    child()->set_parameter(param - child_param_offset_rep, value);
 
189
  }
 
190
  
 
191
  AUDIO_IO::set_parameter(param, value);
 
192
}
 
193
 
 
194
string AUDIO_CLIP_SEQUENCER::get_parameter(int param) const
 
195
{
 
196
  ECA_LOG_MSG(ECA_LOGGER::system_objects, 
 
197
              AUDIO_IO::parameter_get_to_string(param));
 
198
 
 
199
  if (param > 0 && param < static_cast<int>(params_rep.size()) + 1) {
 
200
    if (param > child_param_offset_rep &&
 
201
        is_child_initialized() == true) {
 
202
      params_rep[param - 1] = child()->get_parameter(param - child_param_offset_rep);
 
203
    }
 
204
    return params_rep[param - 1];
 
205
  }
 
206
 
 
207
  return ""; 
 
208
}