Report abuse

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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
Index: src/Makefile.am
===================================================================
--- src/Makefile.am (revision 28129)
+++ src/Makefile.am (working copy)
@@ -198,6 +198,12 @@
 Sound += arch/Sound/RageSoundDriver_OSS.cpp arch/Sound/RageSoundDriver_OSS.h
 endif
 
+if HAVE_PULSE
+Sound += arch/Sound/RageSoundDriver_Pulse.cpp arch/Sound/RageSoundDriver_Pulse.h
+AM_CXXFLAGS += $(PULSE_CFLAGS)
+AM_LDFLAGS += $(PULSE_LIBS)
+endif
+
 if HAVE_ALSA
 Sound += arch/Sound/ALSA9Dynamic.cpp arch/Sound/ALSA9Dynamic.h arch/Sound/ALSA9Functions.h \
  arch/Sound/ALSA9Helpers.cpp arch/Sound/ALSA9Helpers.h \
Index: src/arch/arch_default.h
===================================================================
--- src/arch/arch_default.h (revision 28129)
+++ src/arch/arch_default.h (working copy)
@@ -49,7 +49,7 @@
 #define DEFAULT_INPUT_DRIVER_LIST "X11"
 #endif
 #define DEFAULT_MOVIE_DRIVER_LIST "Theora,FFMpeg,Null"
-#define DEFAULT_SOUND_DRIVER_LIST "ALSA-sw,OSS,Null"
+#define DEFAULT_SOUND_DRIVER_LIST "pulse,ALSA-sw,OSS,Null"
 #else
 #error Which arch?
 #endif
Index: src/arch/Sound/RageSoundDriver_Pulse.cpp
===================================================================
--- src/arch/Sound/RageSoundDriver_Pulse.cpp    (revision 0)
+++ src/arch/Sound/RageSoundDriver_Pulse.cpp    (revision 0)
@@ -0,0 +1,166 @@
+#include "global.h"
+#include "RageSoundDriver_Pulse.h"
+
+#include "RageLog.h"
+#include "RageSound.h"
+#include "RageSoundManager.h"
+#include "RageUtil.h"
+#include "PrefsManager.h"
+
+#include <errno.h>
+#include <string.h>
+#include <pulse/error.h>
+
+REGISTER_SOUND_DRIVER_CLASS( Pulse );
+
+/* samples */
+const int channels = 2;
+const int bytes_per_frame = channels*2;        /* 16-bit */
+const int chunk_order = 12;
+const int num_chunks = 2; //4;
+const int buffersize = num_chunks * (1 << (chunk_order-1)); /* in bytes */
+const int buffersize_frames = buffersize/bytes_per_frame;    /* in frames */
+
+int RageSoundDriver_Pulse::MixerThread_start(void *p)
+{
+    ((RageSoundDriver_Pulse *) p)->MixerThread();
+    return 0;
+}
+
+void RageSoundDriver_Pulse::MixerThread()
+{
+    /* We want to set a higher priority, but Unix only lets root renice
+     * < 0, which is silly.  Give it a try, anyway. */
+    int status = nice( -10 );
+    if( status != -1 )
+        LOG->Trace( "Set MixerThread nice value to %d", status );
+
+    while( !shutdown )
+    {
+        GetData();
+
+        usleep( 10000 );
+    }
+}
+
+void RageSoundDriver_Pulse::SetupDecodingThread()
+{
+    int status = nice( -5 );
+    if( status != -1 )
+        LOG->Trace( "Set DecodingThread nice value to %d", status );
+}
+
+bool RageSoundDriver_Pulse::GetData()
+{
+    const int chunksize = buffersize;
+
+    static int16_t *buf = NULL;
+    if(!buf)
+        buf = new int16_t[chunksize / sizeof(int16_t)];
+
+    this->Mix( buf, chunksize/bytes_per_frame, last_cursor_pos, GetPosition() );
+
+    if (pa_simple_write( s, buf, chunksize, &err ) < 0)
+        return true;    // need more data
+
+    /* Increment last_cursor_pos. */
+    last_cursor_pos += chunksize / bytes_per_frame;
+
+    return false;    // have enough data
+}
+
+int64_t RageSoundDriver_Pulse::GetPosition() const
+{
+    return last_cursor_pos - (buffersize / bytes_per_frame);
+    //return last_cursor_pos;
+}
+
+RageSoundDriver_Pulse::RageSoundDriver_Pulse()
+{
+    s = NULL;
+    shutdown = false;
+    last_cursor_pos = 0;
+}
+
+RString RageSoundDriver_Pulse::Init()
+{
+    samplerate = PREFSMAN->m_iSoundPreferredSampleRate;
+    if( samplerate == 0 )
+        samplerate = 44100;
+
+    spec.format = PA_SAMPLE_S16LE;
+    spec.channels = channels;
+    spec.rate = samplerate;
+
+    //bufattr.maxlength = 4096;
+    bufattr.maxlength = buffersize * 2;
+
+    s = pa_simple_new(
+        NULL,            // default server
+        "StepMania",        // app name
+        PA_STREAM_PLAYBACK,    // playback
+        NULL,            // default device
+        "Audio",        // stream description
+        &spec,            // sample spec
+        NULL,            // default channel map
+        &bufattr,        // h4xed buffering
+        &err            // error code
+    );
+    if (s == NULL)
+        return ssprintf( "RageSoundDriver_Pulse: Couldn't connect to Pulse server: %s", pa_strerror(err) );
+
+    StartDecodeThread();
+
+    MixingThread.SetName( "RageSoundDriver_Pulse" );
+    MixingThread.Create( MixerThread_start, this );
+
+    return "";
+}
+
+RageSoundDriver_Pulse::~RageSoundDriver_Pulse()
+{
+    if( MixingThread.IsCreated() )
+    {
+        /* Signal the mixing thread to quit. */
+        shutdown = true;
+        LOG->Trace("Shutting down mixer thread ...");
+        MixingThread.Wait();
+        LOG->Trace("Mixer thread shut down.");
+    }
+
+    if (s != NULL)
+        pa_simple_free(s);
+}
+
+float RageSoundDriver_Pulse::GetPlayLatency() const
+{
+    //return (1.0f / samplerate) * (buffersize_frames - chunksize_frames);
+    return pa_simple_get_latency(s, NULL) / 1000000.0f;
+    //return 0;
+}
+
+/*
+ * (c) 2002-2004 Glenn Maynard
+ * (c) 2009 Ondrej Hosek
+ * All rights reserved.
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, and/or sell copies of the Software, and to permit persons to
+ * whom the Software is furnished to do so, provided that the above
+ * copyright notice(s) and this permission notice appear in all copies of
+ * the Software and that both the above copyright notice(s) and this
+ * permission notice appear in supporting documentation.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
+ * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
+ * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
+ * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
+ * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
Index: src/arch/Sound/RageSoundDriver_Pulse.h
===================================================================
--- src/arch/Sound/RageSoundDriver_Pulse.h    (revision 0)
+++ src/arch/Sound/RageSoundDriver_Pulse.h    (revision 0)
@@ -0,0 +1,66 @@
+#ifndef RAGE_SOUND_PULSE
+#define RAGE_SOUND_PULSE
+
+#include "RageSoundDriver.h"
+#include "RageThreads.h"
+#include "RageTimer.h"
+
+#include <pulse/simple.h>
+
+class RageSoundDriver_Pulse: public RageSoundDriver
+{
+    pa_simple *s;
+    pa_sample_spec spec;
+    pa_buffer_attr bufattr;
+
+    bool shutdown;
+    int last_cursor_pos;
+    int samplerate;
+
+    static int MixerThread_start(void *p);
+    void MixerThread();
+    RageThread MixingThread;
+
+    int err;
+    
+public:
+    bool GetData();
+    int GetSampleRate() const { return samplerate; }
+
+    /* virtuals: */
+    int64_t GetPosition() const;
+    float GetPlayLatency() const;
+    void SetupDecodingThread();
+
+    RageSoundDriver_Pulse();
+    RString Init();
+    ~RageSoundDriver_Pulse();
+};
+
+#endif
+
+/*
+ * (c) 2002-2004 Glenn Maynard
+ * (c) 2009 Ondrej Hosek
+ * All rights reserved.
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, and/or sell copies of the Software, and to permit persons to
+ * whom the Software is furnished to do so, provided that the above
+ * copyright notice(s) and this permission notice appear in all copies of
+ * the Software and that both the above copyright notice(s) and this
+ * permission notice appear in supporting documentation.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
+ * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
+ * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
+ * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
+ * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
Index: configure.ac
===================================================================
--- configure.ac (revision 28129)
+++ configure.ac (working copy)
@@ -208,6 +208,14 @@
 AC_CHECK_HEADER(stdint.h, , [AC_DEFINE(MISSING_STDINT_H, 1, [stdint.h is missing])])
 AC_CHECK_HEADERS([inttypes.h endian.h machine/endian.h alloca.h])
 
+have_pulse=no
+AC_CHECK_LIB(pulse, pa_stream_new, have_pulse=yes)
+if test x$have_pulse = xyes; then
+  AC_DEFINE(HAVE_PULSE, 1, [pulseaudio support available])
+  AC_SUBST(PULSE_LIBS)
+  AC_SUBST(PULSE_CFLAGS)
+fi
+
 AC_MSG_CHECKING(if cstdlib breaks llabs)
 AC_LANG_PUSH(C++)
 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <stdlib.h>
@@ -227,6 +234,7 @@
 AM_CONDITIONAL(HAVE_ALSA, test x$alsa != xfalse )
 AM_CONDITIONAL(HAVE_GTK, test "$enable_gtk2" != "no" )
 AM_CONDITIONAL(HAVE_OSS, test x$ac_cv_header_sys_soundcard_h = xyes )
+AM_CONDITIONAL(HAVE_PULSE, test x$have_pulse = xyes)
 AM_CONDITIONAL(USE_CRASH_HANDLER, test "$use_crash_handler" = "yes" )
 
 if test x$force_oss = xyes && test x$ac_cv_header_sys_soundcard_h = xyes; then