File: sndrecord.c

package info (click to toggle)
snd 3.4-4
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,148 kB
  • ctags: 12,594
  • sloc: ansic: 86,516; lisp: 3,480; sh: 1,507; makefile: 119
file content (102 lines) | stat: -rw-r--r-- 2,691 bytes parent folder | download
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
/* sndrecord records a sound */

#if defined(HAVE_CONFIG_H)
  #include "config.h"
#endif

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#if (defined(NEXT) || (defined(HAVE_LIBC_H) && (!defined(HAVE_UNISTD_H))))
  #include <libc.h>
#else
  #if (!(defined(_MSC_VER))) && (!(defined(MPW_C)))
    #include <unistd.h>
  #endif
  #include <string.h>
#endif
#include <errno.h>

#include "sndlib.h"

#if MACOS
  #include <console.h>
#endif

#ifdef BEOS
  #define CHANNELS 2
#else
  #define CHANNELS 1
#endif

#ifdef NEXT
  #define BUFFER_SIZE 1024
  #define READS 1
  typedef char indata;
  #define FILE_TYPE NeXT_sound_file
  #define DATA_TYPE SNDLIB_8_MULAW
  #define SAMPLING_RATE 8000
#else
  #define BUFFER_SIZE 4096
  #define READS 10
  typedef short indata;
  #ifdef WINDOZE
    #define FILE_TYPE RIFF_sound_file
    #define DATA_TYPE SNDLIB_8_UNSIGNED
    #define SAMPLING_RATE 22050
  #else
    #if (HAVE_OSS || HAVE_ALSA)
      #define FILE_TYPE RIFF_sound_file
      #define DATA_TYPE SNDLIB_16_LINEAR_LITTLE_ENDIAN
      #define SAMPLING_RATE 22050
    #else
      #define FILE_TYPE AIFF_sound_file
      #define DATA_TYPE SNDLIB_16_LINEAR
      #define SAMPLING_RATE 44100
    #endif
  #endif
#endif

int main(int argc, char *argv[])
{
  int fd,afd,i,err,bytes_per_sample,bytes_per_read;
  float mic_gain[1];
  indata *ibuf;
#if MACOS
  argc = ccommand(&argv);
#endif
  if (argc == 1) {printf("usage: sndrecord outfile\n"); exit(0);}
  afd = -1;
  initialize_sndlib();
  /* make sure the microphone is on */
  mic_gain[0] = 1.0;
  write_audio_state(SNDLIB_MICROPHONE_DEVICE,SNDLIB_AMP_FIELD,0,mic_gain);
  if (CHANNELS == 2) write_audio_state(SNDLIB_MICROPHONE_DEVICE,SNDLIB_AMP_FIELD,1,mic_gain);
  /* open the output sound file */
  bytes_per_sample = mus_format2bytes(DATA_TYPE);
  bytes_per_read = BUFFER_SIZE * bytes_per_sample;
  fd = open_sound_output(argv[1],SAMPLING_RATE,CHANNELS,DATA_TYPE,FILE_TYPE,"created by sndrecord");
  if (fd != -1)
    {
      /* prepare and open the microphone input line */
      ibuf = (indata *)CALLOC(BUFFER_SIZE,sizeof(indata));
      afd = open_audio_input(SNDLIB_MICROPHONE_DEVICE,SAMPLING_RATE,CHANNELS,DATA_TYPE,bytes_per_read);
      if (afd != -1)
	{
	  for (i=0;i<READS;i++)
	    {
	      err = read_audio(afd,(char *)ibuf,bytes_per_read);
	      if (err != SNDLIB_NO_ERROR) {fprintf(stderr,audio_error_name(audio_error())); break;}
	      write(fd,(char *)ibuf,bytes_per_read);
	    }
	  close_audio(afd);
	}
      else 
	fprintf(stderr,audio_error_name(audio_error()));
      close_sound_output(fd,bytes_per_read * READS);
      FREE(ibuf);
    }
  else
    fprintf(stderr,"%s: %s ",argv[1],strerror(errno));
  return(0);
}