summaryrefslogtreecommitdiffhomepage
path: root/src/lingot-audio.c
blob: a0776f65e67c61b1fa98aca17a93815062a1a307 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//-*- C++ -*-
/*
 * lingot, a musical instrument tuner.
 *
 * Copyright (C) 2004-2007  Ibán Cereijo Graña, Jairo Chapela Martínez.
 *
 * This file is part of lingot.
 *
 * lingot is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * lingot 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with lingot; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <sys/soundcard.h>
#include <stdlib.h>

#include "lingot-defs.h"
#include "lingot-audio.h"

LingotAudio *
lingot_audio_new (int channels, int rate, int format, char *fdsp)
{
  LingotAudio *audio;
#ifdef ALSA
  snd_pcm_hw_params_t *hw_params;
  int err;
#endif

  audio = malloc (sizeof (LingotAudio));
  //  int dma  = 4;

#ifdef ALSA

  if ((err =
       snd_pcm_open (&audio->capture_handle, "default", SND_PCM_STREAM_CAPTURE,
		     0)) < 0)
    {
      fprintf (stderr, "cannot open audio device %s (%s)\n",
	       fdsp, snd_strerror (err));
      exit (1);
    }

  if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0)
    {
      fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
	       snd_strerror (err));
      exit (1);
    }

  if ((err = snd_pcm_hw_params_any (audio->capture_handle, hw_params)) < 0)
    {
      fprintf (stderr,
	       "cannot initialize hardware parameter structure (%s)\n",
	       snd_strerror (err));
      exit (1);
    }

  if ((err =
       snd_pcm_hw_params_set_access (audio->capture_handle, hw_params,
				     SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
    {
      fprintf (stderr, "cannot set access type (%s)\n", snd_strerror (err));
      exit (1);
    }

  if ((err =
       snd_pcm_hw_params_set_format (audio->capture_handle, hw_params,
				     SND_PCM_FORMAT_S16_LE)) < 0)
    {
      fprintf (stderr, "cannot set sample format (%s)\n", snd_strerror (err));
      exit (1);
    }

  if ((err =
       snd_pcm_hw_params_set_rate_near (audio->capture_handle, hw_params,
					&rate, 0)) < 0)
    {
      fprintf (stderr, "cannot set sample rate (%s)\n", snd_strerror (err));
      exit (1);
    }

  if ((err =
       snd_pcm_hw_params_set_channels (audio->capture_handle, hw_params,
				       channels)) < 0)
    {
      fprintf (stderr, "cannot set channel count (%s)\n", snd_strerror (err));
      exit (1);
    }

  if ((err = snd_pcm_hw_params (audio->capture_handle, hw_params)) < 0)
    {
      fprintf (stderr, "cannot set parameters (%s)\n", snd_strerror (err));
      exit (1);
    }

  snd_pcm_hw_params_free (hw_params);

  if ((err = snd_pcm_prepare (audio->capture_handle)) < 0)
    {
      fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
	       snd_strerror (err));
      exit (1);
    }

#else

  audio->dsp = open (fdsp, O_RDONLY);
  if (audio->dsp < 0)
    {
      char buff[80];
      sprintf (buff, "Unable to open audio device %s", fdsp);
      perror (buff);
      exit (-1);
    }

  //if (ioctl(audio->dsp, SOUND_PCM_READ_CHANNELS, &channels) < 0)
  if (ioctl (audio->dsp, SNDCTL_DSP_CHANNELS, &channels) < 0)
    {
      perror ("Error setting number of channels");
      exit (-1);
    }

  /*  if (ioctl(audio->dsp, SOUND_PCM_WRITE_CHANNELS, &channels) < 0)
     {
     perror("Error setting number of channels");
     exit(-1);
     } */

  // sample size
  //if (ioctl(audio->dsp, SOUND_PCM_SETFMT, &format) < 0)
  if (ioctl (audio->dsp, SNDCTL_DSP_SETFMT, &format) < 0)
    {
      perror ("Error setting bits per sample");
      exit (-1);
    }

  int fragment_size = 1;
  int DMA_buffer_size = 512;
  int param = 0;

  for (param = 0; fragment_size < DMA_buffer_size; param++)
    fragment_size <<= 1;

  param |= 0x00ff0000;

  if (ioctl (audio->dsp, SNDCTL_DSP_SETFRAGMENT, &param) < 0)
    {
      perror ("Error setting DMA buffer size");
      exit (-1);
    }

  // DMA divisor
  /*  if (ioctl(audio->dsp, SNDCTL_DSP_SUBDIVIDE, &dma) < 0)
     {
     perror("Error setting DMA divisor");
     exit(-1);
     }

     // Rate de muestreo / reproduccion
     if (ioctl(audio->dsp, SOUND_PCM_WRITE_RATE, &rate) < 0)
     {
     perror("Error setting write rate");
     exit(-1);
     } */

  //  if (ioctl(audio->dsp, SOUND_PCM_READ_RATE, &rate) < 0)
  if (ioctl (audio->dsp, SNDCTL_DSP_SPEED, &rate) < 0)
    {
      perror ("Error setting sample rate");
      exit (-1);
    }

  /*
     // non-blocking reads.
     if (fcntl(audio->dsp, F_SETFL, O_NONBLOCK) < 0)
     {
     perror("Error setting non-blocking reads");
     exit(-1);
     }
   */
#endif

  return audio;
}

void
lingot_audio_destroy (LingotAudio * audio)
{
#ifdef ALSA
  snd_pcm_close (audio->capture_handle);
#else
  close (audio->dsp);
  free (audio);
#endif
}

int
lingot_audio_read (LingotAudio * audio, void *buffer, int size)
{
#ifdef ALSA
  return snd_pcm_readi (audio->capture_handle, buffer, size);
#else
  return read (audio->dsp, buffer, size);
#endif
}