File: recs.c

package info (click to toggle)
spectemu 0.94a-19
  • links: PTS, VCS
  • area: contrib
  • in suites: stretch
  • size: 1,300 kB
  • ctags: 3,206
  • sloc: ansic: 15,421; asm: 5,160; sh: 1,533; cpp: 377; makefile: 173
file content (185 lines) | stat: -rw-r--r-- 3,631 bytes parent folder | download | duplicates (9)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <sys/soundcard.h>
#include <sys/ioctl.h>

#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>


#ifdef DEBUG_OUTPUT
#  define DOUT(string) fprintf(stderr, "%s\n", string)
#else
#  define DOUT(string)
#endif


int init_soundcard(long sample_rate, int sample_size, int channels)
{
  char *devname = "/dev/dsp";
  int fd;
  int parm;
  int sr;


  DOUT("Opening the DSP device");
    
  if((fd = open(devname, O_RDONLY)) < 0) {
    fprintf(stderr, "Could not open device '%s'; %s\n", 
	    devname, strerror(errno));
    exit(1);
  }


  parm=0x0100008;
  if(ioctl(fd,SNDCTL_DSP_SETFRAGMENT,&parm)>=0)
    DOUT("  Successful");
  else
    DOUT("  Failed");

  DOUT("Resetting the DSP device");
  if(ioctl(fd,SOUND_PCM_RESET,0)>=0) 
    DOUT("  Successful"); 
  else 
    DOUT("  Failed");
  
  DOUT("Syncing the DSP device");
  if(ioctl(fd,SOUND_PCM_SYNC,0)>=0)
    DOUT("  Successful");
  else
    DOUT("  Failed");
  
  DOUT("Setting the DSP device bits per sample");
  if(ioctl(fd,SOUND_PCM_WRITE_BITS,&sample_size)>=0)
    DOUT("  Successful");
  else
    DOUT("  Failed");
  
  DOUT("Setting the DSP device number of channels");
  if(ioctl(fd,SOUND_PCM_WRITE_CHANNELS,&channels)>=0)
    DOUT("  Successful");
  else
    DOUT("  Failed");
  
   /* Verify the results */
   if((sample_size!=16) && (sample_size!=8))
   {
      fprintf(stderr, 
	      "DSP error: returned a sample size of %d bits.\n",sample_size);
      exit(1);
   }
/*  fprintf(stderr, "Sample size: %i\n", sample_size); */


  sr = sample_rate;
  DOUT("Syncing the DSP device");
  ioctl(fd,SOUND_PCM_SYNC,0);
  
  DOUT("Setting the DSP device sampling rate");
  ioctl(fd,SOUND_PCM_WRITE_RATE,&sr);
  sample_rate=sr;
  if(sample_rate<1000)
    {
      printf("DSP error: returned a sample rate of %ld samples/second.\n",
             sample_rate);
      exit(1);
    }

/*  fprintf(stderr, "Sample rate: %li\n", sample_rate); */

  return fd;
}


int main(int argc, char *argv[])
{
  int sample_size = 8;
  long sample_rate = 8000;
  int channels = 1;

  FILE *fp;
  char *filename;

  int snd;

  int to_quit, recordig;

  int records;
  int rd;
  int at;


#define BUFSIZE 1024

  unsigned char buf[BUFSIZE];

  if(argc < 2) {
    fprintf(stderr, 
	    "usage: %s filename [sample_rate] [sample_size]\n", 
	    argv[0]);
    exit(1);
  }

  filename = argv[1];

  if(strcmp(filename, "-") == 0) fp = stdout;
  else if((fp = fopen(filename, "wb")) == NULL) {
    fprintf(stderr, "%s: Could not open `%s', %s\n", 
	    argv[0], filename, strerror(errno));
    exit(1);
  }

  
  if(argc > 2) {
    sample_rate = atol(argv[2]);
    if(sample_rate < 4000 || sample_rate > 41000) {
      fprintf(stderr, "%s: Illegal sample rate: %s\n", argv[0], argv[2]);
      exit(1);
    }
  }

  if(argc > 3) {
    sample_size = atol(argv[3]);
    if(sample_size != 8 && sample_size != 16) {
      fprintf(stderr, "%s: Illegal sample size: %s\n", argv[0], argv[3]);
      exit(1);
    }
  }


  snd = init_soundcard(sample_rate, sample_size, channels);


  to_quit = 0;
  recordig = 1;
  records = 0;


/*  fprintf(stderr, "\nStart!\n"); */
  
  do {
    for(at = 0; at < BUFSIZE; at += rd) {
      rd = read(snd, buf + at, BUFSIZE - at);
      if(rd < 0) {
	fprintf(stderr, "\nError on soundcard: %s\n", strerror(errno));
	exit(1);
      }
    }
    fwrite(buf, 1, BUFSIZE, fp);
    records++;
/*
     fprintf(stderr, "records written: %4i       \r", records); 
    fflush(stderr);
*/
  } while(!to_quit);
    
/*  fprintf(stderr, "\nStop!\n"); */
  fclose(fp);

  return 0;
}