File: main.cpp

package info (click to toggle)
azr3-jack 1.2.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 952 kB
  • ctags: 771
  • sloc: cpp: 3,590; makefile: 54; sh: 43
file content (394 lines) | stat: -rw-r--r-- 11,764 bytes parent folder | download
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/****************************************************************************
    
    AZR-3 - An organ synth
    
    Copyright (C) 2006-2010 Lars Luthman <lars.luthman@gmail.com>
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 2 as published
    by the Free Software Foundation.
    
    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 01222-1307  USA

****************************************************************************/

#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <stdexcept>

#include "main.hpp"
#include "optionparser.hpp"

using namespace std;


Main::Main(int& argc, char**& argv) : m_ok(false) {
  // parse all non-LASH arguments
  OptionParser op;
  bool help(false);
  bool version(false);
  unsigned preset_no(128);
  string jack_name("AZR-3");
  try {
    op.set_env_prefix("AZR3_JACK_")
      .add_bare("help", "h", help, 
		"Display this help text and exit.")
      .add_bare("version", "v", version, 
		"Display version information and exit.")
      .add("midi-input", "m", "PORT|CLIENT", m_auto_midi,
	   "When used, azr3 will try to connect its MIDI input\n"
	   "port to PORT (if it's a JACK port name) or the\n"
	   "first MIDI output port in CLIENT (if it's a JACK\n"
	   "client name).")
      .add("audio-output", "a", "PORT|CLIENT", m_auto_audio,
	   "When used, azr3 will try to connect its audio\n"
	   "output ports to PORT (if it's a JACK port name) or\n"
	   "the first two audio input ports in CLIENT (if it's\n"
	   "a JACK client name).")
      .add("preset", "p", "NUMBER", preset_no,
	   "Load the preset with the given number instead of\n"
	   "the first available one.")
      .add("jack-name", "j", "NAME", jack_name,
	   "Set the name of the JACK client. The default is\n"
	   "'AZR-3'. Note that JACK may change this name by\n"
	   "e.g. adding a number at the end if needed.")
      .parse_env()
      .parse(argc, argv);
  }
  catch (runtime_error& e) {
    cerr<<e.what()<<endl;
    return;
  }
  
  if (help || version) {
    cout<<argv[0]<<" version "<<PACKAGE_VERSION<<'\n'
	<<"This JACK version ported by Lars Luthman <lars.luthman@gmail.com>\n"
	<<"from the VST original by Rumpelrausch Täips\n\n";
    if (help) {
      cout<<"Program options:\n\n";
      op.print_help(cout);
      op.print_env_help(cout);
    }
    cout<<flush;
    return;
  }
    
  // initialise controls with default values
  float defaults[] = { 0.00, 0.20, 0.20, 0.00, 0.00, 0.75, 0.50, 0.60, 0.60, 
		       0.00, 0.22, 0.00, 1.00, 1.00, 0.00, 0.00, 0.00, 0.00,
		       0.00, 0.00, 0.00, 0.00, 0.30, 0.35, 0.00, 0.00, 0.00,
		       0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
		       0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.40,
		       0.00, 0.66, 0.00, 1.00, 0.00, 0.10, 0.65, 0.05, 0.78,
		       0.50, 0.50, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 };
  memcpy(m_controls, defaults, 63 * sizeof(float));
  memcpy(m_gui_controls, defaults, 63 * sizeof(float));
  for (int i = 0; i < 128; ++i)
    memcpy(m_presets[i].values, defaults, 63 * sizeof(float));
  pthread_mutex_init(&m_engine_wlock, 0);
  pthread_mutex_init(&m_gui_wlock, 0);
  sem_init(&m_engine_changed, 0, 0);
  sem_init(&m_program_changed, 0, 0);
  sem_init(&m_gui_changed, 0, 0);
    
  // load presets
  load_presets(DATADIR "/presets");
  if (getenv("HOME")) {
    char buf[512];
    snprintf(buf, 512, "%s/.azr3_jack_presets", getenv("HOME"));
    load_presets(buf);
  }
    
  // initialise JACK client
  m_jack_client = jack_client_open(jack_name.c_str(), jack_options_t(0), 0);
  m_midi_port = jack_port_register(m_jack_client, "MIDI", 
				   JACK_DEFAULT_MIDI_TYPE, 
				   JackPortIsInput, 0);
  m_left_port = jack_port_register(m_jack_client, "Left", 
				   JACK_DEFAULT_AUDIO_TYPE,
				   JackPortIsOutput, 0);
  m_right_port = jack_port_register(m_jack_client, "Right", 
				    JACK_DEFAULT_AUDIO_TYPE,
				    JackPortIsOutput, 0);
  if (!(m_jack_client && m_midi_port && m_left_port && m_right_port)) {
    cerr<<"Could not initialise JACK client!"<<endl;
    return;
  }
  jack_set_process_callback(m_jack_client, &Main::static_process, this);
    
  // create the engine and connect the controls
  m_engine = new AZR3(jack_get_sample_rate(m_jack_client));
  for (uint32_t i = 0; i < 63; ++i)
    m_engine->connect_port(i, &m_controls[i]);
    
  // create GUI objects, initialise knobs and drawbars, connect signals
  m_kit = new Gtk::Main(argc, argv);
  m_win = new Gtk::Window;
  m_gui = new AZR3GUI;
  for (int i = 0; i < 128; ++i) {
    if (!m_presets[i].empty)
      m_gui->add_program(i, m_presets[i].name.c_str());
  }
  m_gui->signal_set_control.
    connect(sigc::mem_fun(*this, &Main::gui_changed_control));
  m_gui->signal_set_program.
    connect(sigc::mem_fun(*this, &Main::gui_set_preset));
  m_gui->signal_save_program.
    connect(sigc::mem_fun(*this, &Main::gui_save_preset));
  for (uint32_t i = 0; i < 63; ++i)
    m_gui->set_control(i, m_gui_controls[i]);
    
  // try to load the desired preset, if it doesn't work use the first one
  if (preset_no < 128 && !m_presets[preset_no].empty)
    gui_set_preset(preset_no);
  else {
    for (int i = 0; i < 128; ++i) {
      if (!m_presets[i].empty) {
	gui_set_preset(i);
	break;
      }
    }
  }
  
  m_win->set_title("AZR-3");
  m_win->set_resizable(false);
  m_win->add(*m_gui);
  m_win->show_all();
    
  m_ok = true;
}
  
  
void Main::run() {
  m_engine->activate();
  jack_activate(m_jack_client);

  // auto-connect JACK ports if desired
  auto_connect();

  Glib::signal_timeout().
    connect(sigc::bind_return(sigc::mem_fun(*this, &Main::check_changes), 
			      true), 10);
  m_kit->run(*m_win);
  jack_deactivate(m_jack_client);
  m_engine->deactivate();
}
  
  
bool Main::is_ok() const {
  return m_ok;
}

  
void Main::load_presets(char const* file) {
  ifstream fin(file);
  fin>>ws;
  while (fin.good()) {
    int number;
    fin>>number;
    if (number < 0 && number > 127) {
      cerr<<"Invalid program number: "<<number<<endl
	  <<"Skipping the rest of "<<file<<endl;
      break;
    }
    for (int i = 0; i < 63; ++i) {
      float value;
      fin>>value;
      m_presets[number].values[i] = value;
      fin>>ws;
    }
    getline(fin, m_presets[number].name);
    fin>>ws;
    m_presets[number].empty = false;
    cout<<"Loaded program "<<number<<": "<<m_presets[number].name<<endl;
  }
}


void Main::write_presets(char const* file) {
  ofstream fout(file);
  for (unsigned char i = 0; i < 128; ++i) {
    if (!m_presets[i].empty) {
      fout<<int(i);
      for (uint32_t p = 0; p < 63; ++p)
	fout<<" "<<m_presets[i].values[p];
      fout<<" "<<m_presets[i].name<<endl;
    }
  }
}


void Main::gui_changed_control(uint32_t index, float value) {
  pthread_mutex_lock(&m_gui_wlock);
  m_gui_controls[index] = value;
  pthread_mutex_unlock(&m_gui_wlock);
  sem_post(&m_gui_changed);
}
  
  
void Main::gui_set_preset(unsigned char number) {
  if (number < 128) {
    pthread_mutex_lock(&m_gui_wlock);
    for (int i = 0; i < 63; ++i)
      m_gui_controls[i] = m_presets[number].values[i];
    m_program = number;
    pthread_mutex_unlock(&m_gui_wlock);
    sem_post(&m_gui_changed);
    for (int i = 0; i < 63; ++i)
      m_gui->set_control(i, m_presets[number].values[i]);
    m_gui->set_program(number);
  }
}


void Main::gui_save_preset(unsigned char number, const string& name) {
  if (number < 128) {
    m_presets[number].empty = false;
    memcpy(&m_presets[number].values[0], m_gui_controls, sizeof(float) * 63);
    m_presets[number].name = name;
    m_gui->add_program(number, name.c_str());
    m_gui->set_program(number);
    m_program = number;
    if (getenv("HOME")) {
      char buf[512];
      snprintf(buf, 512, "%s/.azr3_jack_presets", getenv("HOME"));
      write_presets(buf);
    }
  }
}
  

bool Main::engine_changed_control(uint32_t index, float value) {
  if (pthread_mutex_trylock(&m_engine_wlock))
    return false;
  m_controls[index] = value;
  pthread_mutex_unlock(&m_engine_wlock);
  sem_post(&m_engine_changed);
  return true;
}
  

void Main::check_changes() {
  static float tmp[63];
  if (!sem_trywait(&m_engine_changed)) {
    while (!sem_trywait(&m_engine_changed));
    pthread_mutex_lock(&m_engine_wlock);
    memcpy(tmp, m_controls, sizeof(float) * 63);
    pthread_mutex_unlock(&m_engine_wlock);
    for (uint32_t i = 0; i < 63; ++i) {
      if (tmp[i] != m_gui_controls[i]) {
	m_gui_controls[i] = tmp[i];
	m_gui->set_control(i, tmp[i]);
      }
    }
  }
  if (!sem_trywait(&m_program_changed)) {
    while (!sem_trywait(&m_program_changed));
    gui_set_preset(m_program);
  }
}


int Main::process(jack_nframes_t nframes) {
    
  // get control changes from the GUI thread
  int sem_val = 0;
  sem_getvalue(&m_gui_changed, &sem_val);
  if (sem_val) {
    if (!pthread_mutex_trylock(&m_gui_wlock)) {
      while (!sem_trywait(&m_gui_changed));
      memcpy(m_controls, m_gui_controls, 63 * sizeof(float));
      pthread_mutex_unlock(&m_gui_wlock);
    }
  }
    
  m_engine->connect_port(63, jack_port_get_buffer(m_midi_port, nframes));
  m_engine->connect_port(64, jack_port_get_buffer(m_left_port, nframes));
  m_engine->connect_port(65, jack_port_get_buffer(m_right_port, nframes));
    
  // XXX It isn't really safe to write automated changes here since the GUI
  //     may be reading from the control values - the mutex isn't locked
  m_engine->run(nframes);
    
  if (m_engine->controls_has_changed())
    sem_post(&m_engine_changed);
  unsigned char prog = m_engine->received_program_change();
  if (prog != 255) {
    m_program = prog;
    sem_post(&m_program_changed);
  }
    
  return 0;
}


int Main::static_process(jack_nframes_t frames, void* arg) {
  return static_cast<Main*>(arg)->process(frames);
}


void Main::auto_connect() {

  const char** port_list;

  // MIDI input
  if (m_auto_midi != "") {

    // if it's a client, connect to the first MIDI output port
    if (m_auto_midi.find(':') == string::npos &&
	(port_list = jack_get_ports(m_jack_client, (m_auto_midi + ":*").c_str(),
				    JACK_DEFAULT_MIDI_TYPE, 
				    JackPortIsOutput)) && port_list[0]) {
      jack_connect(m_jack_client, port_list[0], jack_port_name(m_midi_port));
      free(port_list);
    }

    // if not, connect to that port
    else
      jack_connect(m_jack_client, m_auto_midi.c_str(), 
		   jack_port_name(m_midi_port));
  }

  // audio output
  if (m_auto_audio != "") {
    
    // if it's a client, connect individual ports
    if (m_auto_audio.find(':') == string::npos &&
	(port_list = jack_get_ports(m_jack_client, 
				    (m_auto_audio + ":*").c_str(),
				    JACK_DEFAULT_AUDIO_TYPE, 
				    JackPortIsInput)) && port_list[0]) {
      if (port_list[0])
	jack_connect(m_jack_client, jack_port_name(m_left_port), port_list[0]);
      if (port_list[1])
	jack_connect(m_jack_client, jack_port_name(m_right_port), port_list[1]);
      free(port_list);
    }

    // if not, connect all our ports to that single port
    else {
      jack_connect(m_jack_client, 
		   jack_port_name(m_left_port), m_auto_audio.c_str());
      jack_connect(m_jack_client, 
		   jack_port_name(m_right_port), m_auto_audio.c_str());
    }
  }
}



int main(int argc, char** argv) {
  Main m(argc, argv);
  if (m.is_ok())
    m.run();
  return 0;
}