1 /*
2  * Copyright (C) 2020 Alexandros Theodotou <alex at zrythm dot org>
3  *
4  * This file is part of Zrythm
5  *
6  * Zrythm is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Zrythm is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with Zrythm.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef __AUDIO_RTAUDIO_DEVICE_H__
21 #define __AUDIO_RTAUDIO_DEVICE_H__
22 
23 #include "zrythm-config.h"
24 
25 #ifdef HAVE_RTAUDIO
26 
27 #define RTAUDIO_DEVICE_BUFFER_SIZE 32000
28 
29 #include <stdint.h>
30 
31 #include "zix/ring.h"
32 
33 #include <rtaudio/rtaudio_c.h>
34 
35 typedef struct Port Port;
36 
37 /**
38  * @addtogroup audio
39  *
40  * @{
41  */
42 
43 /**
44  * For readability when passing 0/1 for input
45  * and output.
46  */
47 enum RtAudioDeviceFlow
48 {
49   RTAUDIO_DEVICE_FLOW_OUTPUT,
50   RTAUDIO_DEVICE_FLOW_INPUT,
51 };
52 
53 /**
54  * This is actually a single channel on a device.
55  */
56 typedef struct RtAudioDevice
57 {
58   int           is_input;
59 
60   /** Channel index. */
61   unsigned int  channel_idx;
62 
63   /** Index (device index from RtAudio). */
64   unsigned int  id;
65 
66   char *        name;
67 
68   /** Whether opened or not. */
69   int           opened;
70 
71   /** Whether started (running) or not. */
72   int           started;
73 
74   rtaudio_t     handle;
75 
76   /** Associated port. */
77   Port *        port;
78 
79   /**
80    * Audio data buffer (see
81    * port_prepare_rtaudio_data()).
82    */
83   float         buf[16000];
84 
85   /** Ring buffer for audio data. */
86   ZixRing *     audio_ring;
87 
88   /** Semaphore for blocking writing events while
89    * events are being read. */
90   ZixSem        audio_ring_sem;
91 
92 } RtAudioDevice;
93 
94 RtAudioDevice *
95 rtaudio_device_new (
96   int          is_input,
97   const char * device_name,
98   unsigned int device_id,
99   unsigned int channel_idx,
100   Port *       port);
101 
102 /**
103  * Opens a device allocated with
104  * rtaudio_device_new().
105  *
106  * @param start Also start the device.
107  *
108  * @return Non-zero if error.
109  */
110 int
111 rtaudio_device_open (
112   RtAudioDevice * self,
113   int             start);
114 
115 /**
116  * Close the RtAudioDevice.
117  *
118  * @param free Also free the memory.
119  */
120 int
121 rtaudio_device_close (
122   RtAudioDevice * self,
123   int            free);
124 
125 int
126 rtaudio_device_start (
127   RtAudioDevice * self);
128 
129 int
130 rtaudio_device_stop (
131   RtAudioDevice * self);
132 
133 void
134 rtaudio_device_free (
135   RtAudioDevice *  self);
136 
137 /**
138  * @}
139  */
140 
141 #endif // HAVE_RTAUDIO
142 #endif
143