summaryrefslogtreecommitdiffstats
path: root/akode/plugins/sun_sink/sun_sink.cpp
blob: c8b6c571a84cefe41a94e579b707081a50524f83 (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*  aKode: Sun-Sink

    Copyright (C) 2005 Allan Sandfeld Jensen <kde@carewolf.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include <config.h>

#include <sys/audioio.h>

#include <sys/ioctl.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <alloca.h>

#include <audioframe.h>
#include "sun_sink.h"

#include <iostream>

namespace aKode {

extern "C" { SunSinkPlugin sun_sink; }

struct SunSink::private_data
{
    private_data() : audio_fd(-1), device(0), valid(false) {};
    int audio_fd;
    audio_info_t auinfo;

    const char *device;
    AudioConfiguration config;
    bool valid;
};


SunSink::SunSink()
{
    d = new private_data;
}

SunSink::~SunSink()
{
    close();
    delete d;
}

bool SunSink::open()
{
    const char* device = getenv("AUDIODEV");
    if (!device) device = "/dev/audio";

    if (::access(device, F_OK) != 0) {
        std::cerr << "akode: Device not found: " << device << "\n";
        goto failed;
    }

    d->device = device;

    d->audio_fd = ::open(d->device, O_WRONLY, 0);

    if (d->audio_fd == -1) {
        std::cerr << "akode: No write access to " << device << "\n";
        goto failed;
    }
    d->valid = true;
    return true;

failed:
    d->valid = false;
    return false;
}


void SunSink::close() {
    if (d->audio_fd != -1) ::close(d->audio_fd);
    d->audio_fd = -1;
    d->valid = false;
}

int SunSink::setAudioConfiguration(const AudioConfiguration* config)
{
    d->config = *config;

    AUDIO_INITINFO(&d->auinfo)

    if (ioctl(d->audio_fd, AUDIO_GETINFO, &d->auinfo) < 0)
    {
        d->valid = false;
        return -1;
    }

    int width = config->sample_width;
    if (width < 0 ) width = 16;
    d->auinfo.play.precision = width;

    d->auinfo.play.encoding = AUDIO_ENCODING_LINEAR;

    d->auinfo.play.channels = config->channels;
    d->auinfo.play.sample_rate = config->sample_rate;

    if (ioctl(d->audio_fd, AUDIO_SETINFO, &d->auinfo) < 0)
    {
        d->valid = false;
        return -1;
    }

    if (ioctl(d->audio_fd, AUDIO_GETINFO, &d->auinfo) < 0)
    {
        d->valid = false;
        return -1;
    }

    d->config.sample_width = d->auinfo.play.precision;
    d->config.channels = d->auinfo.play.channels;
    d->config.sample_rate = d->auinfo.play.sample_rate;
    if (d->config.channels <= 2) d->config.channel_config = MonoStereo;

    if (d->config == *config)
        return 0;
    else
        return 1;
}

const AudioConfiguration* SunSink::audioConfiguration() const
{
    return &d->config;
}

bool SunSink::writeFrame(AudioFrame* frame)
{
    if (!d->valid) return false;

    if ( frame->sample_width != d->config.sample_width
      || frame->channels != d->config.channels )
    {
        if (setAudioConfiguration(frame) < 0)
            return false;
    }

    int channels = d->config.channels;
    int length = frame->length;

    int16_t *buffer = (int16_t*)alloca(length*channels*2);
    int16_t** data = (int16_t**)frame->data;
    for(int i = 0; i<length; i++)
        for(int j=0; j<channels; j++)
            buffer[i*channels+j] = data[j][i];

//    std::cerr << "Writing frame\n";
    int status = 0;
    do {
        status = ::write(d->audio_fd, buffer, channels*length*2);
        if (status == -1) {
//            if (errno == EAGAIN) continue;
            if (errno == EINTR) continue;
            return false;
        }
    } while(false);

    return true;
}

} // namespace