1 // -*- c++ -*-
2 
3 /*
4  *  Author: Arvin Schnell
5  */
6 
7 
8 #ifndef pcm_h
9 #define pcm_h
10 
11 
12 #include <stdint.h>
13 
14 
15 class PCM
16 {
17 
18 public:
19 
20     enum pcm_type_t { DEVICE, FILE };
21     enum pcm_mode_t { READ, WRITE };
22     enum pcm_format_t { S8, S16, S32 };
23 
24     /**
25      * Create a PCM object.  The device driver accepts the string "default" as
26      * name for a reasonable default.  buffer_size is in frames.
27      */
28     static PCM* create (const char* name, pcm_type_t type, pcm_mode_t mode,
29 			pcm_format_t format, int channels, int rate,
30 			int buffer_size);
31 
32     /**
33      * Create a PCM object.
34      */
35     static PCM* create (const char* name, pcm_type_t type, pcm_mode_t mode,
36 			pcm_format_t format, int channels, int rate);
37 
38     /**
39      * Destructor.
40      */
41     virtual ~PCM ();
42 
43     /**
44      * Displays some information on cerr.
45      */
46     virtual void info () const = 0;
47 
48     /**
49      * Only useful for files.  Returns size of file in frames.  On error -1 is
50      * returned.
51      */
52     virtual size_t size () = 0;
53 
54     /**
55      * Only useful for files.  Reposition file offset.  On error -1 is
56      * returned.
57      */
58     virtual off_t seek (off_t offset, int whence) = 0;
59 
60     /**
61      * Returns number of frames read (may be 0).  On error -1 is returned.
62      */
63     virtual size_t read (void* buffer, size_t frames) = 0;
64 
65     /**
66      * Returns number of frames written.  On error -1 is returned.
67      */
68     virtual size_t write (void* buffer, size_t frames) = 0;
69 
70     /**
71      * Returns bytes per frame.
72      */
get_bytes_per_frame()73     int get_bytes_per_frame () const { return channels * (1 << format); }
74 
75     /**
76      * Returns buffer size in frames.
77      */
get_buffer_size()78     int get_buffer_size () const { return buffer_size; }
79 
80 protected:
81 
82     PCM (const char* name, pcm_type_t type, pcm_mode_t mode,
83 	 pcm_format_t format, int channels, int rate, int buffer_size);
84 
85     bool ok;
86 
87     char* name;
88     pcm_type_t type;
89     pcm_mode_t mode;
90     pcm_format_t format;
91     int channels;
92     int rate;
93 
94     int buffer_size;
95 
96     void swap_buffer (void* buffer, size_t frames) const;
97 
98     uint16_t uint16_to_le (uint16_t i) const;
99     uint32_t uint32_to_le (uint32_t i) const;
100 
101 };
102 
103 
104 #endif
105