-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjack_flanger.cpp
150 lines (106 loc) · 2.99 KB
/
jack_flanger.cpp
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
#include <iostream>
#include <math.h>
#define BUFFERSIZE 44100
using namespace std;
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <jack/jack.h>
#include <jack/midiport.h>
jack_port_t *input_port;
jack_port_t *output_port;
#define MAXDELAY 2000
jack_default_audio_sample_t delayline[MAXDELAY];
int delay_index=0;
int process(jack_nframes_t nframes, void *arg)
{
jack_default_audio_sample_t *in =
(jack_default_audio_sample_t *) jack_port_get_buffer(input_port, nframes);
jack_default_audio_sample_t *out =
(jack_default_audio_sample_t *) jack_port_get_buffer(output_port,nframes);
for(unsigned int x=0; x<nframes; x++)
{
out[x] = in[x]+delayline[delay_index];
delayline[delay_index]=in[x];
delay_index++;
delay_index%=MAXDELAY;
}
return 0;
} // process()
/*
* shutdown callback may be called by JACK
*/
void jack_shutdown(void *arg)
{
exit(1);
}
int updatesamplerate(jack_nframes_t nframes, void *arg)
{
cout << "Sample rate set to: " << nframes << endl;
return 0;
}
int main()
{
jack_client_t *client;
const char **ports;
// Create a new Jack client
if( (client=jack_client_open("flanger",(jack_options_t)0,0)) == 0)
{
cout << "JACK server not running ?\n";
return 1;
}
// Install the sample processing callback
jack_set_process_callback(client,process,0);
// Install a shutdown routine
jack_on_shutdown(client,jack_shutdown,0); // install a shutdown callback
// Install a routine
jack_set_sample_rate_callback(client,updatesamplerate,0);
// Open an input port
input_port = jack_port_register(client,"in",
JACK_DEFAULT_AUDIO_TYPE,JackPortIsInput, 0);
cout << "Our input port is called: " << jack_port_name(input_port) << endl;
output_port = jack_port_register(client,"out",
JACK_DEFAULT_AUDIO_TYPE,JackPortIsOutput,0);
cout << "Our output port is called: " << jack_port_name(output_port) << endl;
// Get rollin'
if(jack_activate(client))
{
cout << "cannot activate client";
return 1;
}
/*
* The next calls try to auto-connect to a receiving client
*/
// See what ports are available to receive our
if((ports =
jack_get_ports(client,"system|meter",0,JackPortIsInput)) == 0)
//if((ports =
//jack_get_ports(client,0,0,JackPortIsPhysical|JackPortIsInput)) == 0)
{
cout << "Cannot find any physical playback ports\n";
exit(1);
}
// List all ports matching the search criteria
for(int p=0; ports[p] != 0; p++)
{
cout << "Ports found: " << ports[p] << endl;
}
// first output
if(jack_connect(client,jack_port_name(output_port),ports[0]))
{
cout << "Cannot connect output ports\n";
}
// second output
if(jack_connect(client,jack_port_name(output_port),ports[1]))
{
cout << "Cannot connect output ports\n";
}
free(ports); // ports structure no longer needed
/*
* Playback is already running now, let's change some sound parameters
*/
while(1);
jack_client_close(client);
return 1;
}