-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsandbox.c
171 lines (142 loc) · 4.07 KB
/
sandbox.c
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
/* sandbox.c
This file is a part of 'jacksandbox'
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/>.
'jacksandbox' is a simple JACK client for learning and testing audio code.
Copyright 2014 murray foster */
#include<stdio.h> //printf
#include<string.h> //memset
#include<stdlib.h> //exit(0);
#include "rtqueue.h"
#include <jack/jack.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 ;
float dsplogic( float insample ) {
float outsample;
//DSP logic here
outsample = insample;
return outsample;
}
static void*
dspthread(void *arg)
{
while(1) {
rtqueue_enq(fifo_out, dsplogic(rtqueue_deq(fifo_in)));
}
}
static int
process(jack_nframes_t nframes, void *arg)
{
float sample = 0;
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;
}
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(9999999);
fifo_out = rtqueue_init(9999999);
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(void)
{
jack_setup("sandbox");
set_callbacks();
if (activate_client() == 1)
return 1;
allocate_ports(1, 1);
pthread_create(&dspthreadid, NULL, dspthread, 0);
while(1) {sleep(1);};
return 0;
}