File size: 2,292 Bytes
83418c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*
 * recplay.c
 * Phil Burk
 * Minimal record and playback test.
 *
 */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#ifndef __STDC__
/* #include <getopt.h> */
#endif /* __STDC__ */
#include <fcntl.h>
#ifdef __STDC__
#include <string.h>
#else /* __STDC__ */
#include <strings.h>
#endif /* __STDC__ */
#include <sys/soundcard.h>

#define NUM_BYTES   (64*1024)
#define BLOCK_SIZE   (4*1024)

#define AUDIO "/dev/dsp"

char buffer[NUM_BYTES];

int audioDev = 0;

main (int argc, char *argv[])
{
    int   numLeft;
    char *ptr;
    int   num;
    int   samplesize;

    /********** RECORD ********************/
    /* Open audio device. */
    audioDev = open (AUDIO, O_RDONLY, 0);
    if (audioDev == -1)
    {
        perror (AUDIO);
        exit (-1);
    }

    /* Set to 16 bit samples. */
    samplesize = 16;
    ioctl(audioDev, SNDCTL_DSP_SAMPLESIZE, &samplesize);
    if (samplesize != 16)
    {
        perror("Unable to set the sample size.");
        exit(-1);
    }

    /* Record in blocks */
    printf("Begin recording.\n");
    numLeft = NUM_BYTES;
    ptr = buffer;
    while( numLeft >= BLOCK_SIZE )
    {
        if ( (num = read (audioDev, ptr, BLOCK_SIZE)) < 0 )
        {
            perror (AUDIO);
            exit (-1);
        }
        else
        {
            printf("Read %d bytes\n", num);
            ptr += num;
            numLeft -= num;
        }
    }

    close( audioDev );

    /********** PLAYBACK ********************/
    /* Open audio device for writing. */
    audioDev = open (AUDIO, O_WRONLY, 0);
    if (audioDev == -1)
    {
        perror (AUDIO);
        exit (-1);
    }

    /* Set to 16 bit samples. */
    samplesize = 16;
    ioctl(audioDev, SNDCTL_DSP_SAMPLESIZE, &samplesize);
    if (samplesize != 16)
    {
        perror("Unable to set the sample size.");
        exit(-1);
    }

    /* Play in blocks */
    printf("Begin playing.\n");
    numLeft = NUM_BYTES;
    ptr = buffer;
    while( numLeft >= BLOCK_SIZE )
    {
        if ( (num = write (audioDev, ptr, BLOCK_SIZE)) < 0 )
        {
            perror (AUDIO);
            exit (-1);
        }
        else
        {
            printf("Wrote %d bytes\n", num);
            ptr += num;
            numLeft -= num;
        }
    }

    close( audioDev );
}