File: sndfile-jackplay.c

package info (click to toggle)
sndfile-tools 1.03-2
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 1,472 kB
  • ctags: 212
  • sloc: sh: 9,872; ansic: 1,090; makefile: 68
file content (277 lines) | stat: -rw-r--r-- 7,421 bytes parent folder | download | duplicates (3)
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
/*
** Copyright (c) 2007-2009 Erik de Castro Lopo <erikd@mega-nerd.com>
** Copyright (C) 2007 Jonatan Liljedahl <lijon@kymatica.com>
**
** 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 2 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, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include "src/config.h"

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#if HAVE_JACK

#include <math.h>
#include <pthread.h>

#include <jack/jack.h>
#include <jack/ringbuffer.h>

#include <sndfile.h>

#define RB_SIZE (1 << 16)

typedef struct _thread_info
{	pthread_t thread_id ;
	SNDFILE *sndfile ;
	jack_nframes_t pos ;
	jack_client_t *client ;
	unsigned int channels ;
	volatile int can_process ;
	volatile int read_done ;
	volatile int play_done ;
} thread_info_t ;

pthread_mutex_t disk_thread_lock = PTHREAD_MUTEX_INITIALIZER ;
pthread_cond_t data_ready = PTHREAD_COND_INITIALIZER ;

static jack_ringbuffer_t *ringbuf ;
static jack_port_t **output_port ;
static jack_default_audio_sample_t ** outs ;
const size_t sample_size = sizeof (jack_default_audio_sample_t) ;

static int
process (jack_nframes_t nframes, void * arg)
{
	thread_info_t *info = (thread_info_t *) arg ;
	jack_default_audio_sample_t buf [info->channels] ;
	unsigned i, n ;

	if (! info->can_process)
		return 0 ;

	for (n = 0 ; n < info->channels ; n++)
		outs [n] = jack_port_get_buffer (output_port [n], nframes) ;

	for (i = 0 ; i < nframes ; i++)
	{	size_t read_cnt ;

		/* Read one frame of audio. */
		read_cnt = jack_ringbuffer_read (ringbuf, (void*) buf, sample_size*info->channels) ;
		if (read_cnt == 0 && info->read_done)
		{	/* File is done, so stop the main loop. */
			info->play_done = 1 ;
			return 0 ;
			} ;

		/* Update play-position counter. */
		info->pos += read_cnt / (sample_size*info->channels) ;

		/* Output each channel of the frame. */
		for (n = 0 ; n < info->channels ; n++)
			outs [n][i] = buf [n] ;
		} ;

	/* Wake up the disk thread to read more data. */
	if (pthread_mutex_trylock (&disk_thread_lock) == 0)
	{	pthread_cond_signal (&data_ready) ;
		pthread_mutex_unlock (&disk_thread_lock) ;
		} ;

	return 0 ;
} /* process */

static void *
disk_thread (void *arg)
{	thread_info_t *info = (thread_info_t *) arg ;
	sf_count_t buf_avail, read_frames ;
	jack_ringbuffer_data_t vec [2] ;
	size_t bytes_per_frame = sample_size*info->channels ;

	pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL) ;
	pthread_mutex_lock (&disk_thread_lock) ;

	while (1)
	{	jack_ringbuffer_get_write_vector (ringbuf, vec) ;

		read_frames = 0 ;

		if (vec [0].len)
		{	/* Fill the first part of the ringbuffer. */
			buf_avail = vec [0].len / bytes_per_frame ;
			read_frames = sf_readf_float (info->sndfile, (float *) vec [0].buf, buf_avail) ;
			if (vec [1].len)
			{	/* Fill the second part of the ringbuffer? */
				buf_avail = vec [1].len / bytes_per_frame ;
				read_frames += sf_readf_float (info->sndfile, (float *) vec [1].buf, buf_avail) ;
				} ;
			} ;

		if (read_frames == 0)
			break ; /* end of file? */

		jack_ringbuffer_write_advance (ringbuf, read_frames * bytes_per_frame) ;

		/* Tell process that we've filled the ringbuffer. */
		info->can_process = 1 ;

		/* Wait for the process thread to wake us up. */
		pthread_cond_wait (&data_ready, &disk_thread_lock) ;
		} ;

	/* Tell that we're done reading the file. */
	info->read_done = 1 ;
	pthread_mutex_unlock (&disk_thread_lock) ;

	return 0 ;
} /* disk_thread */

static void
jack_shutdown (void *arg)
{	(void) arg ;
	exit (1) ;
} /* jack_shutdown */

static void
print_time (jack_nframes_t pos, int jack_sr)
{	float sec = pos / (1.0 * jack_sr) ;
	int min = sec / 60.0 ;
	fprintf (stderr, "%02d:%05.2f", min, fmod (sec, 60.0)) ;
} /* print_time */

int
main (int narg, char * args [])
{
	SNDFILE *sndfile ;
	SF_INFO sndfileinfo ;
	jack_client_t *client ;
	thread_info_t info ;
	int i, jack_sr ;

	if (narg < 2)
	{	fprintf (stderr, "no soundfile given\n") ;
		return 1 ;
		} ;

	// create jack client
	if ((client = jack_client_new ("jackplay")) == 0)
	{
		fprintf (stderr, "Jack server not running?\n") ;
		return 1 ;
		} ;

	jack_sr = jack_get_sample_rate (client) ;

	/* Open the soundfile. */
	sndfileinfo.format = 0 ;
	sndfile = sf_open (args [1], SFM_READ, &sndfileinfo) ;
	if (sndfile == NULL)
	{	fprintf (stderr, "Could not open soundfile '%s'\n", args [1]) ;
		return 1 ;
		} ;

	fprintf (stderr, "Channels    : %d\nSample rate : %d Hz\nDuration    : ", sndfileinfo.channels, sndfileinfo.samplerate) ;

	print_time (sndfileinfo.frames, sndfileinfo.samplerate) ;
	fprintf (stderr, "\n") ;

	if (sndfileinfo.samplerate != jack_sr)
		fprintf (stderr, "Warning: samplerate of soundfile (%d Hz) does not match jack server (%d Hz).\n", sndfileinfo.samplerate, jack_sr) ;

	/* Init the thread info struct. */
	memset (&info, 0, sizeof (info)) ;
	info.can_process = 0 ;
	info.read_done = 0 ;
	info.play_done = 0 ;
	info.sndfile = sndfile ;
	info.channels = sndfileinfo.channels ;
	info.client = client ;
	info.pos = 0 ;

	/* Set up callbacks. */
	jack_set_process_callback (client, process, &info) ;
	jack_on_shutdown (client, jack_shutdown, 0) ;

	/* Allocate output ports. */
	output_port = calloc (sndfileinfo.channels, sizeof (jack_port_t *)) ;
	outs = calloc (sndfileinfo.channels, sizeof (jack_default_audio_sample_t *)) ;
	for (i = 0 ; i < sndfileinfo.channels ; i++)
	{	char name [16] ;

		snprintf (name, sizeof (name), "out_%d", i + 1) ;
		output_port [i] = jack_port_register (client, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0) ;
		} ;

	/* Allocate and clear ringbuffer. */
	ringbuf = jack_ringbuffer_create (sizeof (jack_default_audio_sample_t) * RB_SIZE) ;
	memset (ringbuf->buf, 0, ringbuf->size) ;

	/* Activate client. */
	if (jack_activate (client))
	{	fprintf (stderr, "Cannot activate client.\n") ;
		return 1 ;
		} ;

	/* Auto connect all channels. */
	for (i = 0 ; i < sndfileinfo.channels ; i++)
	{	char name [64] ;

		snprintf (name, sizeof (name), "alsa_pcm:playback_%d", i + 1) ;

		if (jack_connect (client, jack_port_name (output_port [i]), name))
			fprintf (stderr, "Cannot connect output port %d (%s).\n", i, name) ;
		} ;

	/* Start the disk thread. */
	pthread_create (&info.thread_id, NULL, disk_thread, &info) ;

	/* Sit in a loop, displaying the current play position. */
	while (! info.play_done)
	{	fprintf (stderr, "\r-> ") ;
		print_time (info.pos, jack_sr) ;
		fflush (stdout) ;
		usleep (50000) ;
		} ;

	/* Clean up. */
	jack_client_close (client) ;
	jack_ringbuffer_free (ringbuf) ;
	sf_close (sndfile) ;
	free (outs) ;
	free (output_port) ;

	puts ("") ;

	return 0 ;
} /* main */

#else

int
main (void)
{
	puts (
		"Sorry this program was compiled without libjack (which probably\n"
		"only exists on Linux and Mac OSX) and hence doesn't work."
		) ;

	return 0 ;
} /* main */

#endif