File: BeAudio.c

package info (click to toggle)
csound 1%3A4.23f12-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,132 kB
  • ctags: 17,345
  • sloc: ansic: 101,063; cpp: 7,730; perl: 335; makefile: 318; tcl: 82
file content (105 lines) | stat: -rw-r--r-- 3,237 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
/*              Copyright © 1997–2000  Jens Kilian
 *
 *              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 1, 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.
 *
 *              Be real-time audio interface.  Communicates with server.
 */

#include <OS.h>

#include "BeAudio.h"
#include "CsSvInterface.h"

// Constant data.
static const int32 kPortCapacity = 100;
static const char *kDataPortName[] = {
        "Csound audio input",
        "Csound audio output"
};

// Global variables (ugh).
port_id gADCPort = B_ERROR;
port_id gDACPort = B_ERROR;

static port_id
openConnection(
        int forOutput,
        int nChannels, int sampleFormat, float sampleRate, size_t bufferSize)
{
        port_id serverPort, dataPort;
        struct ServerAudioPort request;
        status_t status;
        const int32 msg = forOutput
                                                 ? kServerNewAudioOutPort
                                                 : kServerNewAudioInPort;

        // Find the server port.
        if ((serverPort = find_port(kServerPortName)) < B_OK) {
                return serverPort;
        }

        // Open the data port.
        if ((dataPort = create_port(kPortCapacity, kDataPortName[forOutput])) < B_OK) {
                return dataPort;
        }

        // Send the request to the server.
        request.mPort = dataPort;
        request.mNumChannels = nChannels;
        request.mSampleFormat = sampleFormat;
        request.mSampleRate = sampleRate;
        request.mBufferSize = bufferSize;
        status = write_port(serverPort, msg, &request, sizeof(request));
        if (status < B_OK) {
                delete_port(dataPort);
                dataPort = (port_id)status;
        }

        return dataPort;
}

port_id
openADCPort(int nChannels, int sampleFormat, float sampleRate,
                           size_t bufferSize)
{
        return gADCPort =
                openConnection(0, nChannels, sampleFormat, sampleRate, bufferSize);
}

port_id
openDACPort(int nChannels, int sampleFormat, float sampleRate,
                           size_t bufferSize)
{
        return gDACPort =
                openConnection(1, nChannels, sampleFormat, sampleRate, bufferSize);
}

void
closeADCPort(void)
{
        if (gADCPort >= B_OK) {
                delete_port(gADCPort);
                gADCPort = B_ERROR;
        }
}

void
closeDACPort(void)
{
        if (gDACPort >= B_OK) {
                delete_port(gDACPort);
                gDACPort = B_ERROR;
        }
}