File: alsa_loopback.cc

package info (click to toggle)
zita-alsa-pcmi 0.2.0-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 292 kB
  • ctags: 270
  • sloc: cpp: 1,499; makefile: 77
file content (132 lines) | stat: -rw-r--r-- 3,266 bytes parent folder | download | duplicates (2)
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
// ----------------------------------------------------------------------------
//
//  Copyright (C) 2003-2012 Fons Adriaensen <fons@linuxaudio.org>
//    
//  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., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ----------------------------------------------------------------------------


#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <zita-alsa-pcmi.h>
#include "pxthread.h"


// --------------------------------------------------------------------------------


static char  playdev [256];
static char  captdev [256];
static int   fsamp;
static int   frsize;
static int   nfrags;


class Audiothr : public Pxthread
{
private:
 
    virtual void thr_main (void);
};


void Audiothr::thr_main (void)
{
    Alsa_pcmi  *D;
    float      *buf;
    int        i, k;

    buf = new float [frsize * 2];

    D = new Alsa_pcmi (playdev, captdev, 0, fsamp, frsize, nfrags, 0);
    if (D->state ()) 
    {
	fprintf (stderr, "Can't open ALSA device\n");
        delete D;
        exit (1);
    }
    if ((D->ncapt () < 2) || (D->nplay () < 2))
    {
	fprintf (stderr, "Expected a stereo device.\n");
        delete D;
        exit (1);
    }
    D->printinfo ();

    D->pcm_start ();
    while (1)
    {
	k = D->pcm_wait ();  
        while (k >= frsize)
       	{
            D->capt_init (frsize);
            D->capt_chan (0, buf + 0, frsize, 2);
            D->capt_chan (1, buf + 1, frsize, 2);              
            D->capt_done (frsize);

            D->play_init (frsize);
            D->play_chan (0, buf + 0, frsize, 2);
            D->play_chan (1, buf + 1, frsize, 2);              
	    for (i = 2; i < D->nplay (); i++) D->clear_chan (i, frsize);
            D->play_done (frsize);

            k -= frsize;
	}
    }
    D->pcm_stop ();

    delete D;
    delete[] buf;
}


// --------------------------------------------------------------------------------


int main (int ac, char *av [])
{
    Audiothr A;

    if (ac < 6)
    {
	fprintf (stderr, "%s <playdev> <captdev> <fsamp> <frsize> <nfrags>\n", av[0]);
        return 1;
    }

    strcpy (playdev, av [1]);
    strcpy (captdev, av [2]);
    fsamp  = atoi (av [3]);
    frsize = atoi (av [4]);
    nfrags = atoi (av [5]);

    if (A.thr_start (SCHED_FIFO, -50, 0x20000))
    {
 	fprintf (stderr, "Can't run in RT mode, trying normal scheduling.\n");
        if (A.thr_start (SCHED_OTHER, 0, 0x20000))
        {
 	    fprintf (stderr, "Can't start audio thread.\n");
	    return 1;
	}
    }

    sleep (1000000);

    return 0;
}


// --------------------------------------------------------------------------------