~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to kmidi/raw_a.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 
 
3
    TiMidity -- Experimental MIDI to WAVE converter
 
4
    Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
 
5
 
 
6
    This program is free software; you can redistribute it and/or modify
 
7
    it under the terms of the GNU General Public License as published by
 
8
    the Free Software Foundation; either version 2 of the License, or
 
9
    (at your option) any later version.
 
10
 
 
11
    This program is distributed in the hope that it will be useful,
 
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
    GNU General Public License for more details.
 
15
 
 
16
    You should have received a copy of the GNU General Public License
 
17
    along with this program; if not, write to the Free Software
 
18
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 
 
20
    raw_audio.c
 
21
 
 
22
    Functions to output raw sound data to a file or stdout.
 
23
 
 
24
*/
 
25
 
 
26
#ifdef __WIN32__
 
27
#include <stdlib.h>
 
28
#include <io.h>
 
29
#include <string.h>
 
30
#else
 
31
#include <unistd.h>
 
32
#endif
 
33
#include <fcntl.h>
 
34
#include <errno.h>
 
35
 
 
36
#include <stdio.h>
 
37
 
 
38
#include "config.h"
 
39
#include "output.h"
 
40
#include "controls.h"
 
41
 
 
42
#if !defined(OPEN_MODE)
 
43
#ifdef __WIN32__
 
44
#define OPEN_MODE O_WRONLY | O_CREAT | O_TRUNC | O_BINARY
 
45
#else
 
46
#define OPEN_MODE O_WRONLY | O_CREAT | O_TRUNC
 
47
#endif
 
48
#endif
 
49
 
 
50
static int open_output(void); /* 0=success, 1=warning, -1=fatal error */
 
51
static void close_output(void);
 
52
static void output_data(int32 *buf, uint32 count);
 
53
static int driver_output_data(unsigned char *buf, uint32 count);
 
54
static void flush_output(void);
 
55
static void purge_output(void);
 
56
static int output_count(uint32 ct);
 
57
 
 
58
/* export the playback mode */
 
59
 
 
60
 
 
61
#undef dpm
 
62
#define dpm raw_play_mode
 
63
 
 
64
PlayMode dpm = {
 
65
  DEFAULT_RATE, PE_16BIT|PE_SIGNED,
 
66
  -1,
 
67
  {0,0,0,0,0},
 
68
  "raw waveform data", 'r',
 
69
  "output.raw",
 
70
  open_output,
 
71
  close_output,
 
72
  output_data,
 
73
  driver_output_data,
 
74
  flush_output,
 
75
  purge_output,
 
76
  output_count
 
77
};
 
78
 
 
79
/*************************************************************************/
 
80
 
 
81
static int open_output(void)
 
82
{
 
83
  if (dpm.encoding & PE_ULAW)
 
84
    dpm.encoding &= ~PE_16BIT;
 
85
 
 
86
  if (!(dpm.encoding & PE_16BIT))
 
87
    dpm.encoding &= ~PE_BYTESWAP;
 
88
 
 
89
  if (dpm.name && dpm.name[0]=='-' && dpm.name[1]=='\0')
 
90
    dpm.fd=1; /* data to stdout */
 
91
  else
 
92
    {
 
93
      /* Open the audio file */
 
94
                dpm.fd=open(dpm.name, OPEN_MODE, 0644);
 
95
      if (dpm.fd<0)
 
96
        {
 
97
          ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s",
 
98
                    dpm.name, sys_errlist[errno]);
 
99
          return -1;
 
100
        }
 
101
    }
 
102
  return 0;
 
103
}
 
104
 
 
105
static int output_count(uint32 ct)
 
106
{
 
107
  return (int)ct;
 
108
}
 
109
 
 
110
static int driver_output_data(unsigned char *buf, uint32 count)
 
111
{
 
112
  return count;
 
113
}
 
114
 
 
115
static void output_data(int32 *buf, uint32 count)
 
116
{
 
117
  if (!(dpm.encoding & PE_MONO)) count*=2; /* Stereo samples */
 
118
  
 
119
  if (dpm.encoding & PE_16BIT)
 
120
    {
 
121
      if (dpm.encoding & PE_BYTESWAP)
 
122
        {
 
123
          if (dpm.encoding & PE_SIGNED)
 
124
            s32tos16x(buf, count);
 
125
          else
 
126
            s32tou16x(buf, count);
 
127
        }
 
128
      else
 
129
        {
 
130
          if (dpm.encoding & PE_SIGNED)
 
131
            s32tos16(buf, count);
 
132
          else 
 
133
            s32tou16(buf, count);
 
134
        }
 
135
      
 
136
      while ((-1==write(dpm.fd, buf, count * 2)) && errno==EINTR)
 
137
        ;
 
138
    }
 
139
  else
 
140
    {
 
141
      if (dpm.encoding & PE_ULAW)
 
142
        {
 
143
          s32toulaw(buf, count);
 
144
        }
 
145
      else
 
146
        {
 
147
          if (dpm.encoding & PE_SIGNED)
 
148
            s32tos8(buf, count);
 
149
          else 
 
150
            s32tou8(buf, count);
 
151
        }
 
152
      
 
153
      while ((-1==write(dpm.fd, buf, count)) && errno==EINTR)
 
154
        ;
 
155
    }
 
156
}
 
157
 
 
158
static void close_output(void)
 
159
{
 
160
  if (dpm.fd != 1) /* We don't close stdout */
 
161
    close(dpm.fd);
 
162
}
 
163
 
 
164
static void flush_output(void) { }
 
165
static void purge_output(void) { }