~ubuntu-branches/ubuntu/breezy/kdemultimedia/breezy

« back to all changes in this revision

Viewing changes to kmidi/arts_a.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2005-03-24 04:48:58 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050324044858-8ff88o9jxej6ii3d
Tags: 4:3.4.0-0ubuntu3
Add kubuntu_02_hide_arts_menu_entries.diff to hide artsbuilder and artscontrol k-menu entries

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
/* 
3
 
        $Id: arts_a.cpp,v 1.12 2001/03/24 09:13:57 wester Exp $
4
 
 
5
 
    TiMidity -- Experimental MIDI to WAVE converter
6
 
    Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
7
 
 
8
 
    This program is free software; you can redistribute it and/or modify
9
 
    it under the terms of the GNU General Public License as published by
10
 
    the Free Software Foundation; either version 2 of the License, or
11
 
    (at your option) any later version.
12
 
 
13
 
    This program is distributed in the hope that it will be useful,
14
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
    GNU General Public License for more details.
17
 
 
18
 
    You should have received a copy of the GNU General Public License
19
 
    along with this program; if not, write to the Free Software
20
 
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
 
 
22
 
    linux_audio.c
23
 
 
24
 
    Functions to play sound on the VoxWare audio driver (Linux or FreeBSD)
25
 
 
26
 
*/
27
 
 
28
 
#define _GNU_SOURCE
29
 
#include <stdio.h>
30
 
#include <unistd.h>
31
 
#include <stdlib.h>
32
 
#include <fcntl.h>
33
 
#include <errno.h>
34
 
 
35
 
#include "config.h"
36
 
#include "output.h"
37
 
#include "controls.h"
38
 
 
39
 
#include <artsc.h>
40
 
 
41
 
static int open_output(void); /* 0=success, 1=warning, -1=fatal error */
42
 
static void close_output(void);
43
 
static void output_data(int32 *buf, uint32 count);
44
 
static int driver_output_data(unsigned char *buf, uint32 count);
45
 
static void flush_output(void);
46
 
static void purge_output(void);
47
 
static int output_count(uint32 ct);
48
 
 
49
 
static arts_stream_t stream;
50
 
static int server_buffer;
51
 
 
52
 
/* export the playback mode */
53
 
 
54
 
#undef dpm
55
 
#define dpm arts_play_mode
56
 
 
57
 
PlayMode dpm = {
58
 
  DEFAULT_RATE, PE_16BIT|PE_SIGNED,
59
 
  -1,
60
 
  {0}, /* default: get all the buffer fragments you can */
61
 
  "Arts device", 'A',
62
 
  "artsd",
63
 
  open_output,
64
 
  close_output,
65
 
  output_data,
66
 
  driver_output_data,
67
 
  flush_output,
68
 
  purge_output,
69
 
  output_count
70
 
};
71
 
 
72
 
/*************************************************************************/
73
 
/* We currently only honor the PE_MONO bit, the sample rate, and the
74
 
   number of buffer fragments. We try 16-bit signed data first, and
75
 
   then 8-bit unsigned if it fails. If you have a sound device that
76
 
   can't handle either, let me know. */
77
 
 
78
 
static int output_count(uint32 ct)
79
 
{
80
 
  extern int b_out_count();
81
 
 
82
 
  int bytes_written = b_out_count();
83
 
  int bytes_buffered = arts_stream_get(stream, ARTS_P_BUFFER_SIZE)
84
 
                     - arts_stream_get(stream, ARTS_P_BUFFER_SPACE)
85
 
                     + server_buffer;  /* see comment in _open */
86
 
  int samples = bytes_written - bytes_buffered;
87
 
  if(samples < 0)
88
 
    samples = 0;
89
 
 
90
 
  if (!(dpm.encoding & PE_MONO)) samples >>= 1;
91
 
  if (dpm.encoding & PE_16BIT) samples >>= 1;
92
 
  return samples;
93
 
}
94
 
 
95
 
static int driver_output_data(unsigned char *buf, uint32 count)
96
 
{
97
 
  return arts_write(stream,buf,count);
98
 
}
99
 
 
100
 
static void output_data(int32 *buf, uint32 count)
101
 
{
102
 
  int ocount;
103
 
 
104
 
  if (!(dpm.encoding & PE_MONO)) count*=2; /* Stereo samples */
105
 
  ocount = (int)count;
106
 
 
107
 
  if (ocount) {
108
 
    if (dpm.encoding & PE_16BIT)
109
 
      {
110
 
        /* Convert data to signed 16-bit PCM */
111
 
        s32tos16(buf, count);
112
 
        ocount *= 2;
113
 
      }
114
 
    else
115
 
      {
116
 
        /* Convert to 8-bit unsigned and write out. */
117
 
        s32tou8(buf, count);
118
 
      }
119
 
  }
120
 
 
121
 
  b_out(dpm.id_character, dpm.fd, (int *)buf, ocount);
122
 
}
123
 
 
124
 
 
125
 
static void close_output(void)
126
 
{
127
 
  arts_close_stream(stream);
128
 
  arts_free();
129
 
}
130
 
 
131
 
static void flush_output(void)
132
 
{
133
 
  output_data(0, 0);
134
 
}
135
 
 
136
 
static void purge_output(void)
137
 
{
138
 
  b_out(dpm.id_character, dpm.fd, 0, -1);
139
 
}
140
 
 
141
 
static int open_output(void) /* 0=success, 1=warning, -1=fatal error */
142
 
{
143
 
  int sample_width, channels, rate;
144
 
  int rc;
145
 
 
146
 
  rc = arts_init();
147
 
  if (rc < 0)
148
 
  {
149
 
    fprintf(stderr, "aRts init failed: %s\n", arts_error_text(rc));
150
 
    return -1;
151
 
  }
152
 
 
153
 
  /* They can't mean these */
154
 
  dpm.encoding &= ~(PE_ULAW|PE_BYTESWAP);
155
 
 
156
 
  /* Set sample width to whichever the user wants. */
157
 
 
158
 
  sample_width=(dpm.encoding & PE_16BIT) ? LE_LONG(16) : LE_LONG(8);
159
 
 
160
 
  if (dpm.encoding & PE_16BIT)
161
 
    dpm.encoding |= PE_SIGNED;
162
 
  else
163
 
    dpm.encoding &= ~PE_SIGNED;
164
 
 
165
 
 
166
 
  /* Try stereo or mono, whichever the user wants. */
167
 
 
168
 
  channels=(dpm.encoding & PE_MONO) ? 1 : 2;
169
 
 
170
 
  /* Set the sample rate */
171
 
  
172
 
  rate=dpm.rate;
173
 
 
174
 
  stream = arts_play_stream(rate, sample_width, channels, "artsctest");
175
 
 
176
 
  arts_stream_set (stream, ARTS_P_BLOCKING, 0);
177
 
 
178
 
  /*
179
 
   * I am not sure if its wise to include the server buffer in our
180
 
   * output sample calculation. While including will give a more realistic
181
 
   * idea of the position we are playing right now (and thus a better
182
 
   * ability to synchronize with the display), it will also give the
183
 
   * certainity "oh well, no worries, there are yet 70kb buffered".
184
 
   *
185
 
   * Well, they are, but if 64kb of these are buffered on the server,
186
 
   * dropout will occur after 6kb anyways.
187
 
   */
188
 
  server_buffer = arts_stream_get(stream, ARTS_P_SERVER_LATENCY) *
189
 
                  rate * (sample_width/8) * channels / 1000;
190
 
  return 0;
191
 
}