~ubuntu-branches/debian/stretch/clalsadrv/stretch

« back to all changes in this revision

Viewing changes to apps/loopback.cc

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2010-04-17 12:25:13 UTC
  • mfrom: (1.1.4 upstream) (2.1.4 experimental)
  • Revision ID: james.westby@ubuntu.com-20100417122513-vsy32s4vwo8vgzvs
Tags: 2.0.0-2
Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2003-2010 Fons Adriaensen <fons@kokkinizita.net>
 
3
    
 
4
    This program is free software; you can redistribute it and/or modify
 
5
    it under the terms of the GNU General Public License as published by
 
6
    the Free Software Foundation; either version 2 of the License, or
 
7
    (at your option) any later version.
 
8
 
 
9
    This program is distributed in the hope that it will be useful,
 
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
    GNU General Public License for more details.
 
13
 
 
14
    You should have received a copy of the GNU General Public License
 
15
    along with this program; if not, write to the Free Software
 
16
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
17
*/
 
18
 
 
19
 
 
20
#include <stdlib.h>
 
21
#include <string.h>
 
22
#include <stdio.h>
 
23
#include <clthreads.h>
 
24
#include <clalsadrv.h>
 
25
 
 
26
 
 
27
// --------------------------------------------------------------------------------
 
28
 
 
29
 
 
30
char          playdev [256];
 
31
char          captdev [256];
 
32
unsigned long fsamp;
 
33
unsigned long frsize;
 
34
unsigned long nfrags;
 
35
 
 
36
 
 
37
class Audiothr : public P_thread
 
38
{
 
39
public:
 
40
 
 
41
    virtual void thr_main (void);
 
42
 
 
43
private:
 
44
 
 
45
    float *buf0;
 
46
    float *buf1;
 
47
 
 
48
};
 
49
 
 
50
 
 
51
void Audiothr::thr_main (void)
 
52
{
 
53
    Alsa_driver *D;
 
54
    unsigned long k;
 
55
 
 
56
    buf0 = new float [frsize];
 
57
    buf1 = new float [frsize];
 
58
 
 
59
    D = new Alsa_driver (playdev, captdev, 0, fsamp, frsize, nfrags);
 
60
    if (D->stat ()) 
 
61
    {
 
62
        fprintf (stderr, "Can't open ALSA device\n");
 
63
        delete D;
 
64
        return;
 
65
    }
 
66
    D->printinfo ();
 
67
 
 
68
    D->pcm_start ();
 
69
    while (1)
 
70
    {
 
71
        k = D->pcm_wait ();  
 
72
        while (k >= frsize)
 
73
        {
 
74
            D->capt_init (frsize);
 
75
            D->capt_chan (0, buf0, frsize);
 
76
            D->capt_chan (1, buf1, frsize);              
 
77
            D->capt_done (frsize);
 
78
 
 
79
            D->play_init (frsize);
 
80
            D->play_chan (0, buf0, frsize);
 
81
            D->play_chan (1, buf1, frsize);              
 
82
            D->play_done (frsize);
 
83
 
 
84
            k -= frsize;
 
85
        }
 
86
    }
 
87
    D->pcm_stop ();
 
88
 
 
89
    delete D;
 
90
}
 
91
 
 
92
 
 
93
// --------------------------------------------------------------------------------
 
94
 
 
95
 
 
96
int main (int ac, char *av [])
 
97
{
 
98
    Audiothr A;
 
99
 
 
100
    if (ac < 6)
 
101
    {
 
102
        fprintf (stderr, "alsa-loopback <playdev><captdev><fsamp><frsize><nfrags>\n");
 
103
        return 1;
 
104
    }
 
105
 
 
106
    strcpy (playdev, av [1]);
 
107
    strcpy (captdev, av [2]);
 
108
    fsamp  = atoi (av [3]);
 
109
    frsize = atoi (av [4]);
 
110
    nfrags = atoi (av [5]);
 
111
 
 
112
    if (A.thr_start (SCHED_FIFO, -50, 0x20000))
 
113
    {
 
114
        fprintf (stderr, "Can't run in RT mode, trying normal scheduling.\n");
 
115
        if (A.thr_start (SCHED_OTHER, 0, 0x20000))
 
116
        {
 
117
            fprintf (stderr, "Can't start audio thread.\n");
 
118
            return 1;
 
119
        }
 
120
    }
 
121
 
 
122
    sleep (1000000);
 
123
 
 
124
    return 0;
 
125
}
 
126
 
 
127
 
 
128
// --------------------------------------------------------------------------------