File: pa_test.c

package info (click to toggle)
libcmtspeechdata 2.1.1%2Bgit20160721~8efc468-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, trixie
  • size: 640 kB
  • sloc: ansic: 4,657; makefile: 156; sh: 8
file content (131 lines) | stat: -rw-r--r-- 4,230 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
/***
  This file is part of PulseAudio.
  PulseAudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published
  by the Free Software Foundation; either version 2.1 of the License,
  or (at your option) any later version.
  PulseAudio 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 Lesser General Public License
  along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <pulse/simple.h>
#include <pulse/error.h>

#define BUFSIZE 1024


int main(int argc, char*argv[]) {
    /* The sample type to use */
    static const pa_sample_spec ss = {
        .format = PA_SAMPLE_S16LE,
        .rate = 4000, /* Requesting 8000 crashes pulseaudio */
        .channels = 1
    };
    static const pa_buffer_attr attr = {
      .fragsize = (uint32_t) 1024,
      .maxlength = (uint32_t) -1,
      .minreq = (uint32_t) 1024,
      .prebuf = (uint32_t) -1,
      .tlength = (uint32_t) 1024,
      /* fragsize / tlength can be 4096 -> pulseaudio CPU drops from 33% CPU to 10%, but latency can be heard */
    };
    pa_simple *r = NULL;
    pa_simple *p = NULL;
    int ret = 1;
    int error;
    const pa_buffer_attr *p_attr = &attr;
    int opt = 0; // | PA_STREAM_ADJUST_LATENCY

    /* Create a new playback stream */
    if (!(p = pa_simple_new(NULL, argv[0], PA_STREAM_PLAYBACK | opt, NULL, "playback", &ss, NULL, p_attr, &error))) {
        fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
        goto finish;
    }
    

    /* Create the recording stream */
    if (!(r = pa_simple_new(NULL, argv[0], PA_STREAM_RECORD | opt, NULL, "record", &ss, NULL, p_attr, &error))) {
        fprintf(stderr, __FILE__": pa_simple_new() failed: %s\n", pa_strerror(error));
        goto finish;
    }

    for (;;) {
        uint8_t buf[BUFSIZE];
        /* Record some data ... */
        if (pa_simple_read(r, buf, sizeof(buf), &error) < 0) {
            fprintf(stderr, __FILE__": pa_simple_read() failed: %s\n", pa_strerror(error));
            goto finish;
        }
        /* ... and play it */
        if (pa_simple_write(p, buf, sizeof(buf), &error) < 0) {
            fprintf(stderr, __FILE__": pa_simple_write() failed: %s\n", pa_strerror(error));
            goto finish;
        }
	
	{
	  pa_usec_t latency_p, latency_r;
	  static pa_usec_t latency_p_avg, latency_r_avg;
	  static int every;

	  if ((latency_p = pa_simple_get_latency(p, &error)) == (pa_usec_t) -1) {
            fprintf(stderr, __FILE__": pa_simple_get_latency() failed: %s\n", pa_strerror(error));
            goto finish;
	  }
	  if ((latency_r = pa_simple_get_latency(r, &error)) == (pa_usec_t) -1) {
            fprintf(stderr, __FILE__": pa_simple_get_latency() failed: %s\n", pa_strerror(error));
            goto finish;
	  }
	  latency_p /= 1000.;
	  latency_r /= 1000.;
	  float factor = 0.01;
	  latency_p_avg = (latency_p_avg * (1 - factor)) + latency_p * factor; 
	  latency_r_avg = (latency_r_avg * (1 - factor)) + latency_r * factor;
	  
	  every++;
	  if (every == 1000) {
	    fprintf(stderr, "\rplayback %7.2f msec avg %6.1f, record %7.2f msec avg %6.1f   ", 
		  (float)latency_p, (float)latency_p_avg, (float)latency_r, (float)latency_r_avg);
	    every = 0;
	  }
	  if (latency_r > 3330000) {
	    fprintf(stderr, "...flush\n");

#if 0
	    if (pa_simple_flush(r, &error) < 0) {
	      fprintf(stderr, __FILE__": pa_simple_flush() failed: %s\n", pa_strerror(error));
	      goto finish;
	    }
#endif
	  }
	}
	

    }

    /* Make sure that every single sample was played */
    if (pa_simple_drain(p, &error) < 0) {
        fprintf(stderr, __FILE__": pa_simple_drain() failed: %s\n", pa_strerror(error));
        goto finish;
    }
    ret = 0;

finish:
    if (r)
        pa_simple_free(r);
    if (p)
        pa_simple_free(p);
    return ret;
}