~ubuntu-branches/debian/sid/acfax/sid

« back to all changes in this revision

Viewing changes to sblaster.c

  • Committer: Bazaar Package Importer
  • Author(s): Hamish Moffatt
  • Date: 2001-12-27 12:07:46 UTC
  • Revision ID: james.westby@ubuntu.com-20011227120746-iz2p5k757bcla8ov
Tags: upstream-981011
ImportĀ upstreamĀ versionĀ 981011

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    ACfax - Fax reception with X11-interface for amateur radio
 
3
    Copyright (C) 1995-1998 Andreas Czechanowski, DL4SDC
 
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 Foundation,
 
17
    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
18
 
 
19
    andreas.czechanowski@ins.uni-stuttgart.de
 
20
*/
 
21
    
 
22
/*
 
23
 * sblaster.c - this file should contain all hardware-dependent functions
 
24
 *              on top of mod_demod.c for the SoundBlaster under linux.
 
25
 */
 
26
 
 
27
#include <stdio.h>
 
28
#include <stdlib.h>
 
29
#include <fcntl.h>
 
30
#include <sys/ioctl.h>
 
31
#include <sys/soundcard.h>
 
32
#include "mod_demod.h"
 
33
#include "sblaster.h"
 
34
#include <unistd.h>
 
35
 
 
36
static int afd, mfd;
 
37
static int blsize;
 
38
static int decsize;
 
39
static int amode;
 
40
static int aspeed;
 
41
static int afilter;
 
42
static int adevi;
 
43
static int amaxval;
 
44
int     rx_level;
 
45
int     dspfd;
 
46
char    *smplptr;
 
47
char    *decptr;
 
48
 
 
49
/* this constants should go to a configuration file in future.... */
 
50
unsigned fm_smplf = (unsigned)(FM_SMPLF * 65536);
 
51
unsigned am_smplf = (unsigned)(AM_SMPLF * 65536);
 
52
 
 
53
static void set_speed(int);
 
54
 
 
55
/* initialize the interface: open it, set it to mono, 8bits per sample,
 
56
 * and allocate space for the input- and output-buffers of the modem-
 
57
 * routines. return a pointer to the decoded data and the number of
 
58
 * values per call to do_receive.
 
59
 */
 
60
void interface_init(char **dta_return, int *cnt_return)
 
61
{
 
62
  int speed = 8000;
 
63
  int bits = 8;
 
64
  int stereo = 0;
 
65
  int linevol = 80;
 
66
  int mastervol = 55;
 
67
#ifdef SBL_16
 
68
  int recsrc = SOUND_MASK_LINE;
 
69
#endif
 
70
  static int inited = 0;
 
71
 
 
72
  /* already initialized ? return values although... */
 
73
  if (inited) {
 
74
    *dta_return = decptr;
 
75
    *cnt_return = decsize;
 
76
    return;
 
77
  }
 
78
 
 
79
  fprintf(stderr, "opening and initializing sound-interface\n");
 
80
  if ((afd = open(DSPDEV, O_RDWR, 0666)) < 0) {
 
81
    perror("open_dsp");
 
82
    exit(1);
 
83
  }
 
84
  dspfd = afd;
 
85
#ifdef SBL_16
 
86
  if ((mfd = open(MIXDEV, O_RDWR, 0666)) < 0) {
 
87
    perror("open_mixer");
 
88
    exit(1);
 
89
  }
 
90
  linevol &= 0xff;
 
91
  linevol += linevol << 8;
 
92
  mastervol &= 0xff;
 
93
  mastervol += mastervol << 8;
 
94
  if ((ioctl(mfd, SOUND_MIXER_WRITE_LINE, &linevol) < 0) ||
 
95
      (ioctl(mfd, SOUND_MIXER_WRITE_VOLUME, &mastervol) < 0) ||
 
96
      (ioctl(mfd, SOUND_MIXER_WRITE_RECSRC, &recsrc) < 0)) {
 
97
    perror("mixer_ioctl");
 
98
    exit(1);
 
99
  }
 
100
  close (mfd);
 
101
#endif
 
102
  if ((ioctl(afd, SNDCTL_DSP_SPEED, &speed) < 0) ||
 
103
      (ioctl(afd, SNDCTL_DSP_SAMPLESIZE, &bits) < 0) ||
 
104
      (ioctl(afd, SNDCTL_DSP_STEREO, &stereo) < 0) ||
 
105
      (ioctl(afd, SNDCTL_DSP_GETBLKSIZE, &blsize) < 0)) {
 
106
    perror("dsp_ioctl");
 
107
    exit(1);
 
108
  }
 
109
  decsize = blsize / 2;
 
110
  /* set up defaults for the modem */
 
111
  amode = MOD_FM;
 
112
  afilter = FIL_MIDL;
 
113
  adevi = 400;
 
114
  set_modem_param (afilter, amaxval, adevi);
 
115
  if (!(decptr = malloc((decsize))) ||
 
116
      !(smplptr = malloc(blsize))) {
 
117
    perror("setup_buffers");
 
118
    exit(1);
 
119
  }
 
120
  printf("block size is %d bytes\n", blsize);
 
121
  *dta_return = decptr;
 
122
  *cnt_return = decsize;
 
123
 
 
124
  inited = -1;
 
125
}
 
126
 
 
127
/* stop the interface, don't let it overrun by no more reading/writing */
 
128
void interface_stop(void)
 
129
{
 
130
  if (ioctl(afd, SNDCTL_DSP_RESET, NULL) < 0) {
 
131
    perror("interface_stop: cannot reset pcm");
 
132
  }
 
133
}
 
134
 
 
135
/* set up the desired mode, the decoded value range and the deviation for
 
136
 * FM. Return the number of demodulated sample-points per second (for APT)
 
137
 */
 
138
void setup_mode(int mode, int maxval, int devi, unsigned *smplf)
 
139
{
 
140
  char *dummy1;
 
141
  int dummy2;
 
142
  int id;
 
143
 
 
144
  /* be sure interface is initialized */
 
145
  interface_init(&dummy1, &dummy2);
 
146
 
 
147
  if ((id = (mode & MOD_BITS))) {
 
148
    switch (id) {
 
149
      case MOD_FM:
 
150
        set_speed(8000);
 
151
        break;
 
152
      case MOD_AM:
 
153
        set_speed(9600);
 
154
        break;
 
155
    }
 
156
    amode = id;
 
157
    fprintf(stderr, "setting demodulator mode to 0x%04x\n", amode);
 
158
  }
 
159
  /* range checking for afilter, adevi and amaxval is done in mod_demod.c */
 
160
  if ((id = mode & FIL_MASK)) {
 
161
    afilter = id;
 
162
    fprintf(stderr, "setting filter selection to 0x%04x\n", afilter);
 
163
  }
 
164
  if (devi) {
 
165
    adevi = devi;
 
166
    fprintf(stderr, "setting deviation to %d\n", adevi);
 
167
  }
 
168
  if (maxval) {
 
169
    amaxval = maxval;
 
170
    fprintf(stderr, "setting max.demod.value to %d\n", amaxval);
 
171
  }
 
172
  if ((mode & FIL_MASK) | (devi) | (maxval)) {
 
173
    set_modem_param (afilter, amaxval, adevi);
 
174
  }
 
175
  /* return the number of samples per second (16 bits int, 16 bits frac) */
 
176
  if (smplf) {
 
177
    switch(amode) {
 
178
      case MOD_AM:
 
179
        *smplf = am_smplf;
 
180
        break;
 
181
      case MOD_FM:
 
182
        *smplf = fm_smplf;
 
183
        break;
 
184
    }
 
185
  }
 
186
}
 
187
 
 
188
/* receive the signal, demodulate it, ant put the demodulated values
 
189
 * into an array starting at decptr with decsize bytes. (1 byte / value)
 
190
 */
 
191
void do_receive(void)
 
192
{
 
193
  int cnt;
 
194
  int hi, lo;
 
195
  int i, sample;
 
196
 
 
197
  cnt = read(afd, smplptr, blsize);
 
198
  if (cnt <= 0) {
 
199
    perror("do_receive");
 
200
    exit(1);
 
201
  }
 
202
  fputc('<', stderr);
 
203
  switch(amode) {
 
204
    case MOD_AM:
 
205
        am_demod(smplptr, cnt, decptr);
 
206
        break;
 
207
    case MOD_FM:
 
208
        fm_demod(smplptr, cnt, decptr);
 
209
        break;
 
210
    default:
 
211
        return;
 
212
  }
 
213
  /* determine the signal level and put it into rx_level (0..255) */
 
214
  lo = 255;
 
215
  hi = 0;
 
216
  for (i = 0; i < blsize / 4; i++)
 
217
  {
 
218
    sample = smplptr[i] & 0xff;
 
219
    if (sample > hi) hi = sample;
 
220
    if (sample < lo) lo = sample;
 
221
  }
 
222
  rx_level = hi - lo;
 
223
}
 
224
 
 
225
/* modulate the sample-values from decptr into smplptr, and transmit them
 
226
 * over the interface. The number of input-values is always decsize.
 
227
 */
 
228
void do_transmit(void)
 
229
{
 
230
  int cnt;
 
231
 
 
232
  switch(amode) {
 
233
    case MOD_FM:
 
234
        fm_modulate(decptr, decsize, smplptr);
 
235
    case MOD_AM:
 
236
        am_modulate(decptr, decsize, smplptr);
 
237
    default:
 
238
        return;
 
239
  }
 
240
  cnt = write(afd, smplptr, blsize);
 
241
  if (cnt <= 0) {
 
242
    perror("do_transmit");
 
243
    exit(1);
 
244
  }
 
245
  fputc('>', stderr);
 
246
}
 
247
 
 
248
/* check for signal level, return a value between 0 and 256 */
 
249
int signal_level(void)
 
250
{
 
251
  char buf[512];
 
252
  int i, p, lo, hi;
 
253
  
 
254
  if (read(afd, buf, 512) != 512) {
 
255
    perror("signal_level: cannot get samples");
 
256
  }
 
257
  if (ioctl(afd, SNDCTL_DSP_SYNC, NULL) < 0) {
 
258
    perror("signal_level: cannot reset pcm");
 
259
  }
 
260
  lo = 255;
 
261
  hi = 0;
 
262
  for (i=0; i<512; i++) {
 
263
    p = buf[i]&0xff;
 
264
    if (p > hi) hi = p;
 
265
    if (p < lo) lo = p;
 
266
  }
 
267
  return (hi-lo);
 
268
}
 
269
 
 
270
/* just assign new value to either the AM or FM sample-frequency,
 
271
   and hold values permanently until next change
 
272
*/
 
273
void tune_frequency(unsigned freq)
 
274
{
 
275
  switch(amode) {
 
276
    case MOD_FM:
 
277
        fm_smplf = freq;
 
278
    case MOD_AM:
 
279
        am_smplf = freq;
 
280
    default:
 
281
        return;
 
282
  }
 
283
}
 
284
 
 
285
/* set a new sample-rate for the DSP-device. We need to restart input
 
286
   in order to do so, because Linux does not update the SB-registers
 
287
   when sending the command, but only on start of input/output
 
288
*/
 
289
static void set_speed(int speed)
 
290
{
 
291
  if (ioctl(afd, SNDCTL_DSP_SPEED, &speed) < 0) {
 
292
    perror("set_speed: cannot set new sample-rate");
 
293
  }
 
294
  fprintf(stderr,"speed set to %d\n", speed);
 
295
  if (ioctl(afd, SNDCTL_DSP_SYNC, NULL) < 0) {
 
296
    perror("set_speed: cannot reset pcm");
 
297
  }
 
298
  aspeed = speed;
 
299
}