File: pulsesound.c

package info (click to toggle)
fuse-emulator 1.6.0%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 9,228 kB
  • sloc: ansic: 93,856; sh: 4,334; perl: 3,921; makefile: 998; yacc: 245; lex: 139
file content (144 lines) | stat: -rw-r--r-- 4,071 bytes parent folder | download
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
/* pulsesound.c: pulseaudio (Linux) sound I/O
   Copyright (c) 2010-2019 Grzegorz Jablonski, Sergio BaldovĂ­

   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.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

*/

#include <config.h>

#include <stdio.h>
#include <string.h>

#include <libspectrum.h>
#include <pulse/error.h>
#include <pulse/simple.h>
#include <pulse/timeval.h>

#include "sound.h"

#define PULSEAUDIO_DEBUG 0

static pa_simple *pulse_s;
static int verbose = 0;

void
sound_lowlevel_end( void )
{
  pa_simple_free( pulse_s );
}

int
sound_lowlevel_init( const char *device, int *freqptr, int *stereoptr )
{
  pa_sample_spec ss;
  pa_buffer_attr buf;
  unsigned int n;
  unsigned int val;
  const char *option;
  char tmp;
  int err;
  int bsize = 0;
  int bdelay = 30; /* default to 30ms */

  /* scan explicitly given parameters */
  option = device;
  while( option && *option ) {
    tmp = '*';
    if( ( err = sscanf( option, " tlength=%ims %n%c", &val, &n, &tmp ) > 0 ) &&
        ( tmp == ',' || strlen( option ) == n ) ) {
      if( val < 1 ) {
        fprintf( stderr, "Bad value for PULSEAUDIO tlength, using default\n" );
      } else {
        bdelay = val;
      }
    } else if( ( err = sscanf( option, " tlength=%i %n%c", &val, &n, &tmp ) > 0 ) &&
        ( tmp == ',' || strlen( option ) == n ) ) {
      if( val < 1 ) {
        fprintf( stderr, "Bad value for PULSEAUDIO tlength, using default\n" );
      } else {
        bsize = val;
      }
    } else if( ( err = sscanf( option, " verbose %n%c", &n, &tmp ) == 1 ) &&
              ( tmp == ',' || ( strlen( option ) == n ) ) ) {
      verbose = 1;
    }

    option += n + ( tmp == ',' );
  }

#if defined WORDS_BIGENDIAN
  ss.format = PA_SAMPLE_S16BE;
#else
  ss.format = PA_SAMPLE_S16LE;
#endif

  ss.channels = ( *stereoptr )? 2 : 1;
  ss.rate = *freqptr;

  /* Reduce latency to 30ms or the user-defined value. pulseaudio does
     not guarantee that */
  buf.tlength = (bsize)? bsize :
                         pa_usec_to_bytes( bdelay * PA_USEC_PER_MSEC, &ss );

  buf.maxlength = buf.tlength * 4;
  buf.minreq = buf.tlength / 4;
  buf.prebuf = (uint32_t) -1;
  buf.fragsize = (uint32_t) -1;

  if( verbose ) {
    fprintf( stdout, "buffer requested: maxlength=%lu, tlength=%lu, prebuf=%lu,"
             " minreq=%lu\n",
             (unsigned long) buf.maxlength,
             (unsigned long) buf.tlength,
             (unsigned long) buf.prebuf,
             (unsigned long) buf.minreq );
  }

  pulse_s = pa_simple_new( NULL, PACKAGE, PA_STREAM_PLAYBACK, NULL,
                           "Spectrum", &ss, NULL, &buf, &err );
  if( pulse_s == NULL ) {
    fprintf( stderr, "pulseaudio: pa_simple_new() failed: %s\n",
             pa_strerror( err ) );
    return 1;
  }

  return 0;
}

void
sound_lowlevel_frame( libspectrum_signed_word *data, int len )
{
  int retval, error;

  /* Measure sound lag */
  if( PULSEAUDIO_DEBUG ) {
    pa_usec_t latency;
    latency = pa_simple_get_latency( pulse_s, &error );
    if( latency == (pa_usec_t) - 1 ) {
      fprintf( stderr, "pulseaudio: pa_simple_get_latency() failed: %s\n",
               pa_strerror( error ) );
    } else {
      fprintf( stderr, "%0.0f ", (float)latency / PA_USEC_PER_MSEC );
    }
  }

  retval = pa_simple_write( pulse_s, data, 2 * len, &error );

  if( verbose && retval < 0 ) {
    fprintf( stderr, "pulseaudio: pa_simple_write() failed: %s\n",
             pa_strerror( error ) );
  }
}