~ubuntu-branches/ubuntu/utopic/cdrdao/utopic

« back to all changes in this revision

Viewing changes to xdao/SoundIF-solaris.cc

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Suffield
  • Date: 2004-06-24 22:33:16 UTC
  • Revision ID: james.westby@ubuntu.com-20040624223316-534onzugaeeyq61j
Tags: upstream-1.1.9
ImportĀ upstreamĀ versionĀ 1.1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  cdrdao - write audio CD-Rs in disc-at-once mode
 
2
 *
 
3
 *  Copyright (C) 1998, 1999  Andreas Mueller <mueller@daneb.ping.de>
 
4
 *
 
5
 *  This program is free software; you can redistribute it and/or modify
 
6
 *  it under the terms of the GNU General Public License as published by
 
7
 *  the Free Software Foundation; either version 2 of the License, or
 
8
 *  (at your option) any later version.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
18
 */
 
19
/*
 
20
 * $Log: SoundIF-solaris.cc,v $
 
21
 * Revision 1.2  2004/02/12 01:13:32  poolshark
 
22
 * Merge from gnome2 branch
 
23
 *
 
24
 * Revision 1.1.1.1.6.2  2004/01/12 20:50:26  poolshark
 
25
 * Added _( and N_( intl macros
 
26
 *
 
27
 * Revision 1.1.1.1.6.1  2004/01/05 00:34:03  poolshark
 
28
 * First checking of gnome2 port
 
29
 *
 
30
 * Revision 1.1.1.1  2003/12/09 05:32:28  denis
 
31
 * Fooya
 
32
 *
 
33
 * Revision 1.1.1.1  2000/02/05 01:40:00  llanero
 
34
 * Uploaded cdrdao 1.1.3 with pre10 patch applied.
 
35
 *
 
36
 * Revision 1.1  1999/05/24 18:07:37  mueller
 
37
 * Initial revision
 
38
 *
 
39
 */
 
40
 
 
41
/*
 
42
 * Sound interface for Solaris. Thanks to Tobias Oetiker <oetiker@ee.ethz.ch>.
 
43
 */
 
44
 
 
45
#include <sys/audioio.h>
 
46
#include <stdio.h>
 
47
#include <assert.h>
 
48
#include <string.h>
 
49
#include <errno.h>
 
50
#include <unistd.h>
 
51
#include <fcntl.h>
 
52
#include <sys/ioctl.h>
 
53
#include <sys/types.h>
 
54
 
 
55
#include <gnome.h>
 
56
 
 
57
#include "SoundIF.h"
 
58
 
 
59
#include "Sample.h"
 
60
#include "util.h"
 
61
 
 
62
class SoundIFImpl {
 
63
public:
 
64
  SoundIFImpl() { dspFd_ = -1; }
 
65
 
 
66
  int setupDevice();
 
67
  int openDevice();
 
68
  void closeDevice();
 
69
 
 
70
  int dspFd_; // sound device
 
71
};
 
72
 
 
73
SoundIF::SoundIF()
 
74
{
 
75
  impl_ = new SoundIFImpl;
 
76
}
 
77
 
 
78
SoundIF::~SoundIF()
 
79
{
 
80
  delete impl_;
 
81
  impl_ = NULL;
 
82
}
 
83
 
 
84
// Initializes sound interface.
 
85
// return: 0: OK
 
86
//         1: sounde device not found
 
87
//         2: cannot setup sound device
 
88
int SoundIF::init()
 
89
{
 
90
  if (impl_->openDevice() != 0)
 
91
    return 1;
 
92
 
 
93
  if (impl_->setupDevice() != 0) {
 
94
    impl_->closeDevice();
 
95
    return 2;
 
96
  }
 
97
 
 
98
  impl_->closeDevice();
 
99
  return 0;
 
100
}
 
101
 
 
102
// Acquires sound device for playing.
 
103
// return 0: OK
 
104
//        1: error occured
 
105
int SoundIF::start()
 
106
{
 
107
  if (impl_->dspFd_ >= 0)
 
108
    return 0; // already opened
 
109
 
 
110
  if (impl_->openDevice() != 0)
 
111
    return 1;
 
112
 
 
113
  if (impl_->setupDevice() != 0) {
 
114
    impl_->closeDevice();
 
115
    return 1;
 
116
  }
 
117
 
 
118
  return 0;
 
119
}
 
120
 
 
121
// Playes given sample buffer.
 
122
// return: 0: OK
 
123
//         1: error occured
 
124
int SoundIF::play(Sample *sbuf, long nofSamples)
 
125
{
 
126
  if (impl_->dspFd_ < 0)
 
127
    return 1;
 
128
 
 
129
  long ret;
 
130
  long len = nofSamples * sizeof(Sample);
 
131
  long nwritten = 0;
 
132
  char *buf = (char *)sbuf;
 
133
 
 
134
  while (len > 0) {
 
135
    ret = write(impl_->dspFd_, buf + nwritten, len);
 
136
 
 
137
    if (ret <= 0)
 
138
      return 1;
 
139
 
 
140
    nwritten += ret;
 
141
    len -= ret;
 
142
  }
 
143
 
 
144
  return 0;
 
145
}
 
146
 
 
147
unsigned long SoundIF::getDelay()
 
148
{
 
149
  return 0;
 
150
}
 
151
 
 
152
// Finishs playing, sound device is released.
 
153
void SoundIF::end()
 
154
{
 
155
  impl_->closeDevice();
 
156
}
 
157
 
 
158
 
 
159
int SoundIFImpl::openDevice()
 
160
{
 
161
  if (dspFd_ >= 0)
 
162
    return 0; // already open
 
163
 
 
164
  if ((dspFd_ = open("/dev/audio", O_WRONLY | O_NONBLOCK)) < 0) {
 
165
    message(-1, _("Cannot open \"/dev/audio\": %s"), strerror(errno));
 
166
    return 1;
 
167
  }
 
168
  /* Clear the non-blocking flag */
 
169
  (void) fcntl(dspFd_, F_SETFL,
 
170
               (fcntl(dspFd_, F_GETFL, 0) & ~(O_NDELAY | O_NONBLOCK)));
 
171
 
 
172
  return 0;
 
173
}
 
174
    
 
175
void SoundIFImpl::closeDevice()
 
176
{
 
177
  if (dspFd_ >= 0) {
 
178
    close(dspFd_);
 
179
    dspFd_ = -1;
 
180
  }
 
181
}
 
182
 
 
183
int SoundIFImpl::setupDevice()
 
184
{
 
185
  struct audio_info auinf;
 
186
 
 
187
  if (dspFd_ < 0)
 
188
    return 1;
 
189
  
 
190
  if (ioctl(dspFd_, AUDIO_GETINFO, &auinf) < 0) {
 
191
    message(-1, _("Cannot get state of audio interface: %s"), strerror(errno));
 
192
    return 1;
 
193
  }
 
194
  auinf.play.sample_rate=44100;
 
195
  auinf.play.channels=2;
 
196
  auinf.play.precision=16;
 
197
  auinf.play.encoding=AUDIO_ENCODING_LINEAR;
 
198
 
 
199
  if (ioctl(dspFd_, AUDIO_SETINFO, &auinf) < 0) {
 
200
    message(-1, _("Cannot setup audio interface: %s"), strerror(errno));
 
201
    return 1;
 
202
  }
 
203
 
 
204
  return 0;
 
205
}