summaryrefslogtreecommitdiff
path: root/src/jackclient.cc
blob: 0f3e2547b656502de6419cbc91d8dcb75a60e9b2 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            jackclient.cc
 *
 *  Sun Jul 20 21:48:44 CEST 2008
 *  Copyright 2008 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of DrumGizmo.
 *
 *  DrumGizmo 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.
 *
 *  DrumGizmo 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 DrumGizmo; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "jackclient.h"

extern "C"
{
  void _wrap_jack_shutdown(void *arg){
    ((JackClient*)arg)->shutdown();}
  int _wrap_jack_process(jack_nframes_t nframes, void *arg){
    return ((JackClient*)arg)->process(nframes);}
  void _wrap_jack_thread_init(void *arg){
    ((JackClient*)arg)->thread_init();}
  void _wrap_jack_freewheel_mode(int freewheel_mode, void *arg){
    ((JackClient*)arg)->freewheel_mode(freewheel_mode);}
  int _wrap_jack_buffer_size(jack_nframes_t nframes, void *arg){
    return ((JackClient*)arg)->buffer_size(nframes);}
  int _wrap_jack_sample_rate(jack_nframes_t nframes, void *arg){
    return ((JackClient*)arg)->sample_rate(nframes);}
  void _wrap_jack_port_registration(jack_port_id_t port, int i, void *arg){
    ((JackClient*)arg)->port_registration(port, i);}
  int _wrap_jack_graph_order(void *arg){
    return ((JackClient*)arg)->graph_order();}
  int _wrap_jack_xrun(void *arg){
    return ((JackClient*)arg)->xrun();}
}  // extern "C"

JackClient::JackClient(size_t num_inputs, size_t num_outputs)
{
	jack_status_t status;

	jack_client = jack_client_open("DrumGizmo", JackNullOption, &status);

	// Setup input port
	midi_port = jack_port_register(jack_client,
                                 "midi_in",
                                 JACK_DEFAULT_MIDI_TYPE,
                                 JackPortIsInput,// | JackPortIsTerminal,
                                 0);

	// Setup input ports
	for(size_t i = 0; i < num_inputs; i++) {
		char port_name[32];
		sprintf(port_name, "input_%i", i + 1);
		jack_port_t *port = jack_port_register(jack_client,
                                           port_name,
                                           JACK_DEFAULT_AUDIO_TYPE,
                                           JackPortIsInput | JackPortIsTerminal,
                                           0);
    input_ports.push_back(port);
	}

	// Setup output ports
	for(size_t i = 0; i < num_outputs; i++) {
		char port_name[32];
		sprintf(port_name, "output_%i", i + 1);
		jack_port_t *port = jack_port_register(jack_client,
                                           port_name,
                                           JACK_DEFAULT_AUDIO_TYPE,
                                           JackPortIsOutput | JackPortIsTerminal,
                                           0);
    output_ports.push_back(port);
	}

	//jack_on_shutdown(jack_client, _wrap_jack_shutdown, this);
  jack_set_process_callback(jack_client, _wrap_jack_process, this);
	//jack_set_thread_init_callback(jack_client, _wrap_jack_thread_init, this);
	//jack_set_freewheel_callback(jack_client, _wrap_jack_freewheel_mode, this);
	//jack_set_buffer_size_callback(jack_client, _wrap_jack_buffer_size, this);
	//jack_set_sample_rate_callback(jack_client, _wrap_jack_sample_rate, this);
	//jack_set_port_registration_callback(jack_client, _wrap_jack_port_registration, this);
	//jack_set_graph_order_callback(jack_client, _wrap_jack_graph_order, this);
	//jack_set_xrun_callback(jack_client, _wrap_jack_xrun, this);


  sample[0] = new Sample("cymbal.wav");
  sample[1] = new Sample("snare.wav");
}

JackClient::~JackClient()
{
	jack_client_close(jack_client);
}

void JackClient::activate()
{
	jack_activate(jack_client);
}

void JackClient::shutdown()
{
}

int JackClient::process(jack_nframes_t nframes)
{
	void *midibuffer = jack_port_get_buffer(midi_port, nframes);
	jack_nframes_t midievents = jack_midi_get_event_count(midibuffer);
	for(jack_nframes_t i = 0; i < midievents; i++) {
		jack_midi_event_t midi_event;
		jack_midi_event_get(&midi_event, midibuffer, i);
    /*
    // Parse midi event
		printf("[ Time: %d  Size: %d  ", midi_event.time, midi_event.size);
		for(size_t j = 0; j < midi_event.size; j++) {
			jack_midi_data_t m = midi_event.buffer[j];
			printf("  Data: %d ", m);
		}
		printf("]\n");
    */
    
    Ports::iterator pi = output_ports.begin();
    while(pi != output_ports.end()) {

      // Create trigger event
      Event event(*pi, sample[midi_event.buffer[0]], midi_event.time);
      events.insert(event);

      pi++;
    }
	}

  jack_midi_clear_buffer(midibuffer);

  // Reset ports 
  Ports::iterator pi = output_ports.begin();
  while(pi != output_ports.end()) {
    
    jack_default_audio_sample_t *buffer;
    buffer = (jack_default_audio_sample_t *)jack_port_get_buffer(*pi, nframes);
    
    for(size_t j = 0; j < nframes; j++) {
      buffer[j] = 0;
    }
    
    pi++;
  }

  Events nextevents;

  //  printf("Events %d\n", events.size());

  // Handle events
  Events::iterator ei = events.begin();
  while(ei != events.end()) {

    Event event = *ei;

    jack_default_audio_sample_t *buffer;
    buffer = (jack_default_audio_sample_t *)jack_port_get_buffer(event.port, nframes);
    
    size_t size = (event.sample->size - event.duration) < nframes ?
      (event.sample->size - event.duration) - event.time : nframes - event.time;
    
    for(size_t j = event.time; j < event.time + size; j++) {
      //memcpy(buffer + event.time, event.sample->data + event.duration, size);
      buffer[j] += event.sample->data[event.duration + j];
    }
    
    if(event.duration + size < event.sample->size) {
      Event e(event.port, event.sample, 0, event.duration + size);
      nextevents.insert(e);
    }
    
    ei++;
  }

  // Remove all dead events
  events = nextevents;

  /*
	for(size_t i = 0; i < output_ports.size(); i++) {
		jack_default_audio_sample_t *buffer;
		buffer = (jack_default_audio_sample_t *)jack_port_get_buffer(output_ports[i], nframes);
		for(int j = 0; j < nframes; j++) {
			if(j == pos) drums[i].samples[0].p = -j;
			offset_t p = drums[i].samples[0].p;
			size_t data_size = params->drums[i].samples[0].data_size;
			jack_default_audio_sample_t *samples = params->drums[i].samples[0].data;
			if(p+j > data_size) buffer[j] = 0;
			else buffer[j] = samples[(j + p) % data_size];
			//      params->drums[i].samples[0].p ++;
		}
		params->drums[i].samples[0].p += nframes;
	}
  */

	return 0;
}

void JackClient::thread_init()
{
}

void JackClient::freewheel_mode(int freewheel_mode)
{
}

int JackClient::buffer_size(jack_nframes_t nframes)
{
  return 0;
}

int JackClient::sample_rate(jack_nframes_t nframes)
{
  return 0;
}

void JackClient::port_registration(jack_port_id_t port, int i)
{
}

int JackClient::graph_order()
{
  return 0;
}

int JackClient::xrun()
{
  return 0;
}