~ubuntu-branches/ubuntu/lucid/jack-audio-connection-kit/lucid

« back to all changes in this revision

Viewing changes to tools/tw.c

  • Committer: Bazaar Package Importer
  • Author(s): Luca Falavigna
  • Date: 2008-12-06 11:05:15 UTC
  • mfrom: (4.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20081206110515-xa9v9pajr9jqvfvg
Tags: 0.115.6-1ubuntu1
* Merge from Debian unstable, remaining Ubuntu changes:
  - Redirect stderr in bash completion (Debian #504488).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** @file simple_client.c
 
2
 *
 
3
 * @brief This simple client demonstrates the basic features of JACK
 
4
 * as they would be used by many applications.
 
5
 */
 
6
 
 
7
#include <stdio.h>
 
8
#include <errno.h>
 
9
#include <unistd.h>
 
10
#include <stdlib.h>
 
11
#include <string.h>
 
12
 
 
13
#include <jack/jack.h>
 
14
 
 
15
jack_port_t *input_port;
 
16
jack_port_t *output_port;
 
17
jack_client_t *client;
 
18
 
 
19
/* a simple state machine for this client */
 
20
volatile enum {
 
21
        Init,
 
22
        Run,
 
23
        Exit
 
24
} client_state = Init;
 
25
 
 
26
/**
 
27
 * The process callback for this JACK application is called in a
 
28
 * special realtime thread once for each audio cycle.
 
29
 *
 
30
 * This client follows a simple rule: when the JACK transport is
 
31
 * running, copy the input port to the output.  When it stops, exit.
 
32
 */
 
33
int
 
34
_process (jack_nframes_t nframes)
 
35
{
 
36
        jack_default_audio_sample_t *in, *out;
 
37
        jack_transport_state_t ts = jack_transport_query(client, NULL);
 
38
        
 
39
        if (ts == JackTransportRolling) {
 
40
                
 
41
                if (client_state == Init)
 
42
                        client_state = Run;
 
43
                
 
44
                in = jack_port_get_buffer (input_port, nframes);
 
45
                out = jack_port_get_buffer (output_port, nframes);
 
46
                memcpy (out, in,
 
47
                        sizeof (jack_default_audio_sample_t) * nframes);
 
48
                
 
49
        } else if (ts == JackTransportStopped) {
 
50
                
 
51
                if (client_state == Run)
 
52
                        client_state = Exit;
 
53
        }
 
54
 
 
55
        return 0;      
 
56
}
 
57
 
 
58
int
 
59
process (jack_nframes_t nframes, void* arg)
 
60
{
 
61
        jack_client_t* client = (jack_client_t*) arg;
 
62
 
 
63
        while ((nframes = jack_thread_wait (client, _process (nframes))) != 0);
 
64
 
 
65
        return 0;
 
66
}
 
67
 
 
68
/**
 
69
 * JACK calls this shutdown_callback if the server ever shuts down or
 
70
 * decides to disconnect the client.
 
71
 */
 
72
void
 
73
jack_shutdown (void *arg)
 
74
{
 
75
        exit (1);
 
76
}
 
77
 
 
78
int
 
79
main (int argc, char *argv[])
 
80
{
 
81
        const char **ports;
 
82
        const char *client_name;
 
83
        const char *server_name = NULL;
 
84
        jack_options_t options = JackNullOption;
 
85
        jack_status_t status;
 
86
 
 
87
        if (argc >= 2) {                /* client name specified? */
 
88
                client_name = argv[1];
 
89
                if (argc >= 3) {        /* server name specified? */
 
90
                        server_name = argv[2];
 
91
                        options |= JackServerName;
 
92
                }
 
93
        } else {                        /* use basename of argv[0] */
 
94
                client_name = strrchr(argv[0], '/');
 
95
                if (client_name == 0) {
 
96
                        client_name = argv[0];
 
97
                } else {
 
98
                        client_name++;
 
99
                }
 
100
        }
 
101
 
 
102
        /* open a client connection to the JACK server */
 
103
 
 
104
        client = jack_client_open (client_name, options, &status, server_name);
 
105
        if (client == NULL) {
 
106
                fprintf (stderr, "jack_client_open() failed, "
 
107
                         "status = 0x%2.0x\n", status);
 
108
                if (status & JackServerFailed) {
 
109
                        fprintf (stderr, "Unable to connect to JACK server\n");
 
110
                }
 
111
                exit (1);
 
112
        }
 
113
        if (status & JackServerStarted) {
 
114
                fprintf (stderr, "JACK server started\n");
 
115
        }
 
116
        if (status & JackNameNotUnique) {
 
117
                client_name = jack_get_client_name(client);
 
118
                fprintf (stderr, "unique name `%s' assigned\n", client_name);
 
119
        }
 
120
 
 
121
        /* tell the JACK server to call `process()' whenever
 
122
           there is work to be done.
 
123
        */
 
124
 
 
125
        jack_set_process_callback (client, process, client);
 
126
 
 
127
        /* tell the JACK server to call `jack_shutdown()' if
 
128
           it ever shuts down, either entirely, or if it
 
129
           just decides to stop calling us.
 
130
        */
 
131
 
 
132
        jack_on_shutdown (client, jack_shutdown, 0);
 
133
 
 
134
        /* display the current sample rate. 
 
135
         */
 
136
 
 
137
        printf ("engine sample rate: %" PRIu32 "\n",
 
138
                jack_get_sample_rate (client));
 
139
 
 
140
        /* create two ports */
 
141
 
 
142
        input_port = jack_port_register (client, "input",
 
143
                                         JACK_DEFAULT_AUDIO_TYPE,
 
144
                                         JackPortIsInput, 0);
 
145
        output_port = jack_port_register (client, "output",
 
146
                                          JACK_DEFAULT_AUDIO_TYPE,
 
147
                                          JackPortIsOutput, 0);
 
148
 
 
149
        if ((input_port == NULL) || (output_port == NULL)) {
 
150
                fprintf(stderr, "no more JACK ports available\n");
 
151
                exit (1);
 
152
        }
 
153
 
 
154
        /* Tell the JACK server that we are ready to roll.  Our
 
155
         * process() callback will start running now. */
 
156
 
 
157
        if (jack_activate (client)) {
 
158
                fprintf (stderr, "cannot activate client");
 
159
                exit (1);
 
160
        }
 
161
 
 
162
        /* Connect the ports.  You can't do this before the client is
 
163
         * activated, because we can't make connections to clients
 
164
         * that aren't running.  Note the confusing (but necessary)
 
165
         * orientation of the driver backend ports: playback ports are
 
166
         * "input" to the backend, and capture ports are "output" from
 
167
         * it.
 
168
         */
 
169
 
 
170
        ports = jack_get_ports (client, NULL, NULL,
 
171
                                JackPortIsPhysical|JackPortIsOutput);
 
172
        if (ports == NULL) {
 
173
                fprintf(stderr, "no physical capture ports\n");
 
174
                exit (1);
 
175
        }
 
176
 
 
177
        if (jack_connect (client, ports[0], jack_port_name (input_port))) {
 
178
                fprintf (stderr, "cannot connect input ports\n");
 
179
        }
 
180
 
 
181
        free (ports);
 
182
        
 
183
        ports = jack_get_ports (client, NULL, NULL,
 
184
                                JackPortIsPhysical|JackPortIsInput);
 
185
        if (ports == NULL) {
 
186
                fprintf(stderr, "no physical playback ports\n");
 
187
                exit (1);
 
188
        }
 
189
 
 
190
        if (jack_connect (client, jack_port_name (output_port), ports[0])) {
 
191
                fprintf (stderr, "cannot connect output ports\n");
 
192
        }
 
193
 
 
194
        free (ports);
 
195
 
 
196
        /* keep running until the transport stops */
 
197
 
 
198
        while (client_state != Exit) {
 
199
                sleep (1);
 
200
        }
 
201
 
 
202
        jack_client_close (client);
 
203
        exit (0);
 
204
}