-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjackdiff.c
More file actions
290 lines (230 loc) · 7 KB
/
jackdiff.c
File metadata and controls
290 lines (230 loc) · 7 KB
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
/* jackdiff.c
This file is a part of 'jackdiff'
This program 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 3 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, see <http://www.gnu.org/licenses/>.
'jackdiff' is a simple JACK client for learning and testing audio code.
Copyright 2014 murray foster */
#include <jack/jack.h>
#include<lo/lo.h>
#include<math.h>
#include<stdio.h> //printf
#include<string.h> //memset
#include<stdlib.h> //exit(0);
#include<unistd.h>
#include "global_t.h"
#include "osc.h"
#include "osc_handlers.h"
#include "rtqueue.h"
static jack_default_audio_sample_t ** outs ;
static jack_default_audio_sample_t ** ins ;
int samples_can_process = 0;
rtqueue_t *fifo_in;
rtqueue_t *fifo_out;
jack_client_t *client=NULL;
pthread_t dspthreadid;
int jack_sr;
const size_t sample_size = sizeof (jack_default_audio_sample_t) ;
jack_port_t **output_port ;
static jack_port_t **input_port ;
int global_audio_sample_size = 0;
int global_audio_sample_count = 0;
float *global_audio_buffer_in = NULL;
float *global_audio_buffer_out = NULL;
/* BEGIN dsp_logic() STATE PARAMETERS */
/* END dsp_logic() STATE PRARMETERS */
void print_usage() {
printf("Usage: jackdiff [options] [arg]\n\n");
printf("Options:\n"
" -h, --help displays this menu\n"
" -l, --length number of samples to process (default 256)\n\n");
} /* print_usage */
float
dsplogic(float insample) {
/* BEGIN DSP CODE */
/* END DSP CODE */
return insample;
} /* dsplogic */
int global_audio_capture_flag = 0;
void*
dspthread(void *arg)
{
FILE *fp_in, *fp_out;
float insample, outsample = 0.0;
fp_in = fopen("output/signal_in.txt", "a");
fp_out = fopen("output/signal_out.txt", "a");
/* BEGIN INIT DSP STATE */
/* END INIT DSP STATE */
while(1) {
if( global_audio_sample_count >= global_audio_sample_size ) {
printf("writing..\n");
for(int i=0; i < global_audio_sample_count; i++) {
fprintf(fp_in, "%f\n", global_audio_buffer_in[i]);
fprintf(fp_out, "%f\n", global_audio_buffer_out[i]);
}
lo_address lo_addr_send = lo_address_new("127.0.0.1", "10001");
lo_send(lo_addr_send, "/jackdiff/isready", "i", 0);
free(lo_addr_send);
printf("isready message sent..\n");
fclose(fp_in);
fclose(fp_out);
printf("exiting..\n");
exit(0);
}
insample = rtqueue_deq(fifo_in);
if( global_audio_capture_flag ) {
global_audio_buffer_in[global_audio_sample_count] = insample;
}
outsample = dsplogic(insample);
rtqueue_enq(fifo_out, outsample);
if( global_audio_capture_flag ) {
global_audio_buffer_out[global_audio_sample_count] = outsample;
global_audio_sample_count += 1;
}
}
} /* dspthread */
static int
process(jack_nframes_t nframes, void *arg)
{
float sample = 0.0f;
unsigned i, n, x;
int sample_count = 0;
/* allocate all output buffers */
for(i = 0; i < 1; i++)
{
outs [i] = jack_port_get_buffer (output_port[i], nframes);
memset(outs[i], 0, nframes * sample_size);
ins [i] = jack_port_get_buffer (input_port[i], nframes);
}
for ( i = 0; i < nframes; i++) {
if( !rtqueue_isempty(fifo_out) ) {
outs[0][i] = rtqueue_deq(fifo_out);
}
else {
outs[0][i]=0.0f;
}
rtqueue_enq(fifo_in,ins[0][i]);
}
return 0 ;
} /* process */
static void
jack_shutdown (void *arg)
{
(void) arg ;
exit (1) ;
} /* jack_shutdown */
void
allocate_ports(int channels, int channels_in)
{
int i = 0;
char name [16];
/* allocate output ports */
output_port = calloc (channels, sizeof (jack_port_t *)) ;
outs = calloc (channels, sizeof (jack_default_audio_sample_t *)) ;
for (i = 0 ; i < channels; i++)
{
snprintf (name, sizeof (name), "out_%d", i + 1) ;
output_port [i] = jack_port_register (client, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0) ;
}
/* allocate input ports */
size_t in_size = channels_in * sizeof (jack_default_audio_sample_t*);
input_port = (jack_port_t **) malloc (sizeof (jack_port_t *) * channels_in);
ins = (jack_default_audio_sample_t **) malloc (in_size);
memset(ins, 0, in_size);
for( i = 0; i < channels_in; i++)
{
snprintf( name, sizeof(name), "in_%d", i + 1);
input_port[i] = jack_port_register(client, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
}
} /* allocate_ports */
int
set_callbacks()
{
/* Set up callbacks. */
jack_set_process_callback (client, process, NULL) ;
jack_on_shutdown (client, jack_shutdown, 0) ;
return 0;
} /* set_callbacks */
int
jack_setup(char *client_name)
{
/* create jack client */
if ((client = jack_client_open(client_name, JackNullOption,NULL)) == 0)
{
fprintf (stderr, "Jack server not running?\n") ;
return 1 ;
} ;
/* store jack server's samplerate */
jack_sr = jack_get_sample_rate (client) ;
fifo_in = rtqueue_init(9999);
fifo_out = rtqueue_init(9999);
return 0;
} /* jack_setup */
int
activate_client()
{
/* Activate client. */
if (jack_activate (client))
{
fprintf (stderr, "Cannot activate client.\n") ;
return 1 ;
}
return 0;
} /* activate_client */
int main(int argc, char *argv[])
{
char *signal_length;
char *store_flag=NULL;
char *store_input=NULL;
printf("processing cli args\n");
/* process command-line input */
for(int c=1; c<argc; c++)
{
store_flag = argv[c];
if( store_flag != NULL )
{
if( !strcmp(store_flag,"-l") ||
!strcmp(store_flag,"--length")) {
printf("found --length arg\n");
store_input=argv[c+1];
printf("stored store_input\n");
signal_length=store_input;
printf("stored signal_length\n");
}
/* reset temporarily stored flag&input */
store_input=NULL;
store_flag=NULL;
}
}
if( signal_length == NULL )
signal_length="256";
global_audio_sample_size = strtol(signal_length, NULL, 10);
printf("global_audio_sample_size: %d\n", global_audio_sample_size);
global_audio_buffer_in = malloc(sizeof(float) * global_audio_sample_size);
global_audio_buffer_out = malloc(sizeof(float) * global_audio_sample_size);
printf("osc_setup()\n");
osc_setup("10000", "10001");
printf("jack_setup()\n");
jack_setup("jackdiff");
printf("jack_setup() ran\n");
set_callbacks();
printf("set_callbacks() ran\n");
if (activate_client() == 1)
return 1;
printf("activate_client() ran\n");
allocate_ports(1, 1);
printf("allocated ports\n");
pthread_create(&dspthreadid, NULL, dspthread, 0);
printf("pthread_create() called\n");
while(1) {
sleep(1);
};
return 0;
}