1 /* GSequencer - Advanced GTK Sequencer
2  * Copyright (C) 2005-2020 Joël Krähemann
3  *
4  * This file is part of GSequencer.
5  *
6  * GSequencer is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU 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  * GSequencer 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 General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSequencer.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef __AGS_CORE_AUDIO_PORT_H__
21 #define __AGS_CORE_AUDIO_PORT_H__
22 
23 #include <glib.h>
24 #include <glib-object.h>
25 
26 #include <ags/ags_api_config.h>
27 
28 #ifdef AGS_WITH_CORE_AUDIO
29 #include <AudioToolbox/AudioToolbox.h>
30 
31 #include <AudioUnit/AudioUnit.h>
32 #include <AudioUnit/AUComponent.h>
33 #include <AudioUnit/AudioComponent.h>
34 
35 #include <CoreMIDI/CoreMIDI.h>
36 #endif
37 
38 #include <ags/libags.h>
39 
40 G_BEGIN_DECLS
41 
42 #define AGS_TYPE_CORE_AUDIO_PORT                (ags_core_audio_port_get_type())
43 #define AGS_CORE_AUDIO_PORT(obj)                (G_TYPE_CHECK_INSTANCE_CAST((obj), AGS_TYPE_CORE_AUDIO_PORT, AgsCoreAudioPort))
44 #define AGS_CORE_AUDIO_PORT_CLASS(class)        (G_TYPE_CHECK_CLASS_CAST(class, AGS_TYPE_CORE_AUDIO_PORT, AgsCoreAudioPort))
45 #define AGS_IS_CORE_AUDIO_PORT(obj)             (G_TYPE_CHECK_INSTANCE_TYPE ((obj), AGS_TYPE_CORE_AUDIO_PORT))
46 #define AGS_IS_CORE_AUDIO_PORT_CLASS(class)     (G_TYPE_CHECK_CLASS_TYPE ((class), AGS_TYPE_CORE_AUDIO_PORT))
47 #define AGS_CORE_AUDIO_PORT_GET_CLASS(obj)      (G_TYPE_INSTANCE_GET_CLASS(obj, AGS_TYPE_CORE_AUDIO_PORT, AgsCoreAudioPortClass))
48 
49 #define AGS_CORE_AUDIO_PORT_GET_OBJ_MUTEX(obj) (&(((AgsCoreAudioPort *) obj)->obj_mutex))
50 
51 #define AGS_CORE_AUDIO_PORT_DEFAULT_CACHE_BUFFER_SIZE (4096)
52 
53 typedef struct _AgsCoreAudioPort AgsCoreAudioPort;
54 typedef struct _AgsCoreAudioPortClass AgsCoreAudioPortClass;
55 
56 /**
57  * AgsCoreAudioPortFlags:
58  * @AGS_CORE_AUDIO_PORT_ADDED_TO_REGISTRY: the CoreAudio port was added to registry, see #AgsConnectable::add_to_registry()
59  * @AGS_CORE_AUDIO_PORT_CONNECTED: indicates the port was connected by calling #AgsConnectable::connect()
60  * @AGS_CORE_AUDIO_PORT_REGISTERED: the port was registered
61  * @AGS_CORE_AUDIO_PORT_IS_AUDIO: the port provides audio data
62  * @AGS_CORE_AUDIO_PORT_IS_MIDI: the port provides midi data
63  * @AGS_CORE_AUDIO_PORT_IS_OUTPUT: the port does output
64  * @AGS_CORE_AUDIO_PORT_IS_INPUT: the port does input
65  *
66  * Enum values to control the behavior or indicate internal state of #AgsCoreAudioPort by
67  * enable/disable as flags.
68  */
69 typedef enum{
70   AGS_CORE_AUDIO_PORT_ADDED_TO_REGISTRY  = 1,
71   AGS_CORE_AUDIO_PORT_CONNECTED          = 1 <<  1,
72   AGS_CORE_AUDIO_PORT_REGISTERED         = 1 <<  2,
73   AGS_CORE_AUDIO_PORT_IS_AUDIO           = 1 <<  3,
74   AGS_CORE_AUDIO_PORT_IS_MIDI            = 1 <<  4,
75   AGS_CORE_AUDIO_PORT_IS_OUTPUT          = 1 <<  5,
76   AGS_CORE_AUDIO_PORT_IS_INPUT           = 1 <<  6,
77 }AgsCoreAudioPortFlags;
78 
79 struct _AgsCoreAudioPort
80 {
81   GObject gobject;
82 
83   guint flags;
84 
85   GRecMutex obj_mutex;
86 
87   GObject *core_audio_client;
88 
89   AgsUUID *uuid;
90 
91   GObject *core_audio_device;
92 
93   gchar *port_uuid;
94   gchar *port_name;
95 
96   guint pcm_channels;
97 
98   guint samplerate;
99   guint buffer_size;
100   guint format;
101 
102   gboolean use_cache;
103   guint cache_buffer_size;
104 
105   guint current_cache;
106   guint completed_cache;
107   guint cache_offset;
108   void **cache;
109 
110 #ifdef AGS_WITH_CORE_AUDIO
111   AudioQueueRef aq_ref;
112   AudioStreamBasicDescription data_format;
113 
114   AudioQueueRef record_aq_ref;
115   AudioStreamBasicDescription record_format;
116 
117   AudioQueueBufferRef buf_ref[16];
118   AudioQueueBufferRef record_buf_ref[16];
119 
120   MIDIClientRef *midi_client;
121   MIDIPortRef *midi_port;
122 #else
123   gpointer aq_ref;
124   gpointer data_format;
125 
126   gpointer buffer;
127 
128   gpointer midi_client;
129   gpointer midi_port;
130 #endif
131 
132   unsigned int midi_port_number;
133 
134   volatile gboolean output_running;
135   volatile gboolean input_running;
136   volatile gboolean is_empty;
137   volatile guint queued;
138 };
139 
140 struct _AgsCoreAudioPortClass
141 {
142   GObjectClass gobject;
143 };
144 
145 GType ags_core_audio_port_get_type();
146 
147 gboolean ags_core_audio_port_test_flags(AgsCoreAudioPort *core_audio_port, guint flags);
148 void ags_core_audio_port_set_flags(AgsCoreAudioPort *core_audio_port, guint flags);
149 void ags_core_audio_port_unset_flags(AgsCoreAudioPort *core_audio_port, guint flags);
150 
151 GList* ags_core_audio_port_find(GList *core_audio_port,
152 				gchar *port_name);
153 
154 void ags_core_audio_port_register(AgsCoreAudioPort *core_audio_port,
155 				  gchar *port_name,
156 				  gboolean is_audio, gboolean is_midi,
157 				  gboolean is_output);
158 void ags_core_audio_port_unregister(AgsCoreAudioPort *core_audio_port);
159 
160 void ags_core_audio_port_set_format(AgsCoreAudioPort *core_audio_port,
161 				    guint format);
162 void ags_core_audio_port_set_samplerate(AgsCoreAudioPort *core_audio_port,
163 					guint samplerate);
164 void ags_core_audio_port_set_pcm_channels(AgsCoreAudioPort *core_audio_port,
165 					  guint pcm_channels);
166 void ags_core_audio_port_set_buffer_size(AgsCoreAudioPort *core_audio_port,
167 					 guint buffer_size);
168 
169 void ags_core_audio_port_set_cache_buffer_size(AgsCoreAudioPort *core_audio_port,
170 					       guint cache_buffer_size);
171 
172 guint ags_core_audio_port_get_latency(AgsCoreAudioPort *core_audio_port);
173 
174 AgsCoreAudioPort* ags_core_audio_port_new(GObject *core_audio_client);
175 
176 G_END_DECLS
177 
178 #endif /*__AGS_CORE_AUDIO_PORT_H__*/
179