annotate Plugins/Output/arts/arts_helper/arts_helper.c @ 1209:e67fad649f58 trunk

[svn] - make it prettier
author nenolod
date Wed, 14 Jun 2006 00:16:06 -0700
parents e9509e909193
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
810
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
1 /*
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
2 * aRts ouput plugin for xmms
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
3 *
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
4 * Copyright (C) 2000,2003 Haavard Kvaalen <havardk@xmms.org>
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
5 *
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
6 * Licenced under GNU GPL version 2.
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
7 *
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
8 * Audacious port by Giacomo Lozito from develia.org
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
9 *
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
10 */
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
11
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
12 #include "arts_helper.h"
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
13
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
14 #include <stdlib.h>
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
15 #include <stdio.h>
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
16 #include <string.h>
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
17 #include <unistd.h>
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
18 #include <errno.h>
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
19 #include <signal.h>
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
20 #include <sys/select.h>
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
21
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
22 #include <artsc.h>
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
23
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
24 #define FALSE 0
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
25 #define TRUE (!FALSE)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
26 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
27 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
28
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
29 #ifdef WORDS_BIGENDIAN
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
30 #define INT16_FROM_LE(val) ((val & 0xff) << 8 | (val & 0xff00) >> 8)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
31 #define INT16_TO_LE(val) ((val & 0xff) << 8 | (val & 0xff00) >> 8)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
32 #else
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
33 #define INT16_FROM_LE(val) (val)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
34 #define INT16_TO_LE(val) (val)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
35 #endif
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
36
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
37 /* This is not quite portable, but should be ok anyway */
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
38 typedef short int16;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
39
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
40 static arts_stream_t handle;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
41
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
42 static int going, paused, inited;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
43 static struct params_info output_params;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
44
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
45 struct {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
46 int left, right;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
47 } volume = {100, 100};
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
48
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
49 struct params_info
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
50 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
51 int resolution;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
52 int frequency;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
53 int channels;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
54
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
55 /* Cache */
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
56 int bps;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
57 };
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
58
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
59 static struct {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
60 unsigned int bsize, latency, psize;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
61 } arts_params;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
62
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
63 static struct {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
64 char *ptr;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
65 int size;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
66 int rd, wr;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
67 } ring_buffer;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
68
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
69 static void artsxmms_set_params(struct params_info *params, int resolution, int rate, int nch)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
70 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
71 params->frequency = rate;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
72 params->channels = nch;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
73
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
74 params->bps = rate * nch;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
75 params->resolution = resolution;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
76 if (resolution == 16)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
77 params->bps *= 2;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
78 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
79
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
80 static int artsxmms_free(void)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
81 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
82 if (ring_buffer.rd > ring_buffer.wr)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
83 return ring_buffer.rd - ring_buffer.wr - 1;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
84 return ring_buffer.size - (ring_buffer.wr - ring_buffer.rd);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
85 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
86
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
87 static int artsxmms_buffer_used(void)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
88 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
89 return ring_buffer.size - artsxmms_free();
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
90 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
91
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
92 static int artsxmms_get_output_latency(void)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
93 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
94 int latency;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
95 long long w;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
96
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
97 if (!going)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
98 return 0;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
99
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
100 w = artsxmms_buffer_used();
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
101 w += arts_params.bsize - arts_stream_get(handle, ARTS_P_BUFFER_SPACE);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
102 latency = (w * 1000) / output_params.bps;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
103
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
104 if (!paused)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
105 latency += arts_params.latency;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
106
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
107 return latency;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
108 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
109
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
110 static int artsxmms_playing(void)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
111 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
112 if (!going)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
113 return FALSE;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
114
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
115 if (!paused)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
116 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
117 int t;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
118 t = arts_stream_get(handle, ARTS_P_BUFFER_SPACE);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
119 return t < arts_params.bsize - arts_params.psize;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
120 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
121
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
122 return TRUE;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
123 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
124
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
125 static void artsxmms_open_stream(struct params_info *params, int buffer_time)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
126 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
127 int buffersize = buffer_time * params->bps / 1000;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
128 handle = arts_play_stream(params->frequency, params->resolution,
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
129 params->channels, "XMMS");
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
130 arts_params.bsize = arts_stream_set(handle, ARTS_P_BUFFER_SIZE,
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
131 buffersize);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
132 arts_params.latency = arts_stream_get(handle, ARTS_P_SERVER_LATENCY);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
133 arts_params.psize = arts_stream_get(handle, ARTS_P_PACKET_SIZE);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
134 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
135
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
136 static void artsxmms_set_volume(int l, int r)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
137 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
138 volume.left = l;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
139 volume.right = r;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
140 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
141
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
142 static void volume_adjust(void* data, int length)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
143 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
144 int i;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
145
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
146 if ((volume.left == 100 && volume.right == 100) ||
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
147 (output_params.channels == 1 &&
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
148 (volume.left == 100 || volume.right == 100)))
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
149 return;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
150
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
151 if (output_params.resolution == 16)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
152 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
153 int16 *ptr = data;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
154 if (output_params.channels == 2)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
155 for (i = 0; i < length; i += 4)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
156 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
157 *ptr = INT16_TO_LE(INT16_FROM_LE(*ptr) *
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
158 volume.left / 100);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
159 ptr++;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
160 *ptr = INT16_TO_LE(INT16_FROM_LE(*ptr) *
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
161 volume.right / 100);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
162 ptr++;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
163 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
164 else
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
165 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
166 int vol = MAX(volume.left, volume.right);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
167 for (i = 0; i < length; i += 2, ptr++)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
168 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
169 *ptr = INT16_TO_LE(INT16_FROM_LE(*ptr) *
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
170 vol / 100);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
171 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
172 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
173 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
174 else
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
175 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
176 unsigned char *ptr = data;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
177 if (output_params.channels == 2)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
178 for (i = 0; i < length; i += 2)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
179 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
180 *ptr = *ptr * volume.left / 100;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
181 ptr++;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
182 *ptr = *ptr * volume.right / 100;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
183 ptr++;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
184 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
185 else
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
186 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
187 int vol = MAX(volume.left, volume.right);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
188 for (i = 0; i < length; i++, ptr++)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
189 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
190 *ptr = *ptr * vol / 100;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
191 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
192 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
193 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
194 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
195
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
196 static void artsxmms_write(char *ptr, int length)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
197 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
198 int cnt;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
199
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
200 /* FIXME: Check that length is not too large? */
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
201 while (length > 0)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
202 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
203 cnt = MIN(length, ring_buffer.size - ring_buffer.wr);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
204 memcpy(ring_buffer.ptr + ring_buffer.wr, ptr, cnt);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
205 ring_buffer.wr = (ring_buffer.wr + cnt) % ring_buffer.size;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
206 length -= cnt;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
207 ptr += cnt;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
208 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
209 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
210
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
211 static void artsxmms_write_arts(void)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
212 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
213 int ret, cnt, space;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
214 char *ptr;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
215
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
216 if (ring_buffer.wr == ring_buffer.rd || paused)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
217 return;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
218
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
219 space = arts_stream_get(handle, ARTS_P_BUFFER_SPACE);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
220
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
221 while (space > 0 && ring_buffer.wr != ring_buffer.rd)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
222 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
223 if (ring_buffer.wr > ring_buffer.rd)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
224 cnt = MIN(space, ring_buffer.wr - ring_buffer.rd);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
225 else
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
226 cnt = MIN(space, ring_buffer.size - ring_buffer.rd);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
227
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
228 ptr = ring_buffer.ptr + ring_buffer.rd;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
229
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
230 volume_adjust(ptr, cnt);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
231 ret = arts_write(handle, ptr, cnt);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
232 if (ret < 0)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
233 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
234 /* FIXME: handle this better? */
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
235 fprintf(stderr, "artsxmms_write(): write error: %s\n",
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
236 arts_error_text(ret));
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
237 return;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
238 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
239
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
240 ring_buffer.rd = (ring_buffer.rd + cnt) % ring_buffer.size;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
241 space -= cnt;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
242 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
243 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
244
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
245 static void artsxmms_close(void)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
246 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
247 going = 0;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
248 arts_close_stream(handle);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
249 arts_free();
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
250 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
251
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
252 static int read_all(int fd, void *buf, size_t count)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
253 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
254 size_t left = count;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
255 int r;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
256 do {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
257 r = read(fd, buf, left);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
258 if (r < 0)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
259 return -1;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
260 left -= r;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
261 buf = (char *)buf + r;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
262 } while (left > 0 && r > 0);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
263 return count - left;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
264 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
265
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
266 static int write_all(int fd, const void *buf, size_t count)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
267 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
268 size_t left = count;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
269 int w;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
270 do {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
271 w = write(fd, buf, left);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
272
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
273 if (w < 0)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
274 return -1;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
275 left -= w;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
276 buf = (char *)buf + w;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
277 } while (left > 0 && w > 0);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
278 return count - left;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
279 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
280
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
281
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
282 static int init_ring_buffer(int size)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
283 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
284 free(ring_buffer.ptr);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
285 /* Make the ring buffer always end on a sample boundary */
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
286 size -= size % 4;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
287 ring_buffer.size = size;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
288 ring_buffer.ptr = malloc(size);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
289 ring_buffer.rd = 0;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
290 ring_buffer.wr = 0;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
291 if (ring_buffer.ptr == NULL)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
292 return -1;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
293 return 0;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
294 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
295
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
296 static int helper_init(struct init_data *init)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
297 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
298 int buffer_time = MAX(init->buffer_time, 50);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
299 if (init->version != HELPER_VERSION) {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
300 fprintf(stderr,
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
301 "Fatal: Version mismatch between arts output plugin and\n"
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
302 " audacious-arts-helper program.\n");
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
303 return -1;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
304 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
305 if (!inited)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
306 return -1;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
307 artsxmms_set_params(&output_params, init->resolution, init->rate,
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
308 init->nchannels);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
309
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
310 if (init_ring_buffer((buffer_time * 2 * output_params.bps) / 1000))
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
311 return -1;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
312
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
313 if (handle)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
314 arts_close_stream(handle);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
315 artsxmms_open_stream(&output_params, buffer_time);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
316
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
317 going = 1;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
318 return 0;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
319 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
320
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
321 static int process_cmd(int fd)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
322 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
323 struct command inp;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
324 struct response outp;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
325 void *data = NULL;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
326 int retval = 0;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
327
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
328 if (read_all(fd, &inp, sizeof(inp)) != sizeof(inp)) {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
329 fprintf(stderr, "read short, giving up\n");
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
330 return -1;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
331 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
332 if (inp.data_length > 0) {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
333 data = malloc(inp.data_length);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
334 if (data == NULL)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
335 return -1;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
336 if (read_all(fd, data, inp.data_length) != inp.data_length) {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
337 fprintf(stderr, "data read short, giving up\n");
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
338 return -1;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
339 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
340 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
341 outp.cmd = inp.cmd;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
342 outp.status = STATUS_OK;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
343 outp.data = 0;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
344 /* fprintf(stderr, "Recieved %d; ", inp.cmd); */
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
345 switch (inp.cmd) {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
346 case CMD_QUIT:
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
347 artsxmms_close();
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
348 retval = 1;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
349 break;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
350 case CMD_INIT:
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
351 if (inp.data_length != sizeof (struct init_data))
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
352 outp.status = STATUS_FAILED;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
353 else if (helper_init(data))
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
354 outp.status = STATUS_FAILED;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
355 break;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
356 case CMD_PAUSE:
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
357 paused = inp.data;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
358 break;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
359 case CMD_SET_VOLUME: {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
360 int *vol = data;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
361 if (inp.data_length < 2 * sizeof(int)) {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
362 outp.status = STATUS_FAILED;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
363 break;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
364 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
365 artsxmms_set_volume(vol[0], vol[1]);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
366 break;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
367 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
368 case CMD_WRITE:
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
369 artsxmms_write(data, inp.data_length);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
370 break;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
371 case CMD_FREE:
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
372 outp.data = artsxmms_free();
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
373 break;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
374 case CMD_GET_OUTPUT_LATENCY:
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
375 outp.data = artsxmms_get_output_latency();
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
376 break;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
377 case CMD_QUERY_PLAYING:
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
378 outp.data = artsxmms_playing();
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
379 break;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
380 default:
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
381 outp.status = STATUS_UNKNOWN;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
382 fprintf(stderr, "Unknown command %d\n", inp.cmd);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
383 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
384 free(data);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
385 if (write_all(fd, &outp, sizeof (outp)) != sizeof (outp))
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
386 return -1;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
387 return retval;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
388 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
389
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
390
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
391 static int main_loop(int fd)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
392 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
393 int retval = 0, sr;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
394 struct timeval timeout;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
395 fd_set rdfs;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
396
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
397 for (;;) {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
398 FD_ZERO(&rdfs);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
399 FD_SET(fd, &rdfs);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
400 timeout.tv_sec = 0;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
401 timeout.tv_usec = 20000;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
402 sr = select(fd + 1, &rdfs, NULL, NULL, &timeout);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
403 if (sr < 0) {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
404 fprintf(stderr, "audacious-arts-helper select failed: %s\n",
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
405 strerror(errno));
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
406 retval = -1;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
407 break;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
408 } else if (sr) {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
409 int p = process_cmd(fd);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
410 if (p < 0) {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
411 fprintf(stderr, "cmd failed\n");
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
412 retval = 1;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
413 break;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
414 } else if (p)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
415 break;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
416 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
417
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
418 artsxmms_write_arts();
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
419 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
420 return retval;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
421 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
422
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
423 int main(int argc, char **argv)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
424 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
425 int fd, err, ret;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
426
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
427 if (argc != 2 || (fd = atoi(argv[1])) < 1)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
428 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
429 fprintf(stderr, "Usage: audacious-arts-helper fd\n");
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
430 return 1;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
431 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
432
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
433 inited = 1;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
434
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
435 if ((err = arts_init()) != 0)
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
436 {
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
437 fprintf(stderr, "artsxmms_open(): Unable to initialize aRts: %s\n",
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
438 arts_error_text(err));
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
439 inited = 0;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
440 }
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
441
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
442 ret = main_loop(fd);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
443 close(fd);
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
444 /* fprintf(stderr, "helper exits\n"); */
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
445 return ret < 0;
e9509e909193 [svn] ported xmms-arts 0.7.1 to audacious and integrated it in the build process
giacomo
parents:
diff changeset
446 }