-
Notifications
You must be signed in to change notification settings - Fork 279
/
recorder.c
175 lines (140 loc) · 4.11 KB
/
recorder.c
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Allegro audio recording
*/
#include "allegro5/allegro_audio.h"
#include "allegro5/internal/aintern_audio.h"
#include "allegro5/internal/aintern_audio_cfg.h"
#include "allegro5/internal/aintern.h"
ALLEGRO_DEBUG_CHANNEL("audio")
ALLEGRO_STATIC_ASSERT(recorder,
sizeof(ALLEGRO_AUDIO_RECORDER_EVENT) <= sizeof(ALLEGRO_EVENT));
/* Function: al_create_audio_recorder
*/
ALLEGRO_AUDIO_RECORDER *al_create_audio_recorder(size_t fragment_count,
unsigned int samples, unsigned int frequency,
ALLEGRO_AUDIO_DEPTH depth, ALLEGRO_CHANNEL_CONF chan_conf)
{
size_t i;
ALLEGRO_AUDIO_RECORDER *r;
ASSERT(_al_kcm_driver);
if (!_al_kcm_driver->allocate_recorder) {
ALLEGRO_ERROR("Audio driver does not support recording.\n");
return false;
}
r = al_calloc(1, sizeof(*r));
if (!r) {
ALLEGRO_ERROR("Unable to allocate memory for ALLEGRO_AUDIO_RECORDER\n");
return false;
}
r->fragment_count = fragment_count;
r->samples = samples;
r->frequency = frequency,
r->depth = depth;
r->chan_conf = chan_conf;
r->sample_size = al_get_channel_count(chan_conf) * al_get_audio_depth_size(depth);
r->fragments = al_malloc(r->fragment_count * sizeof(uint8_t *));
if (!r->fragments) {
al_free(r);
ALLEGRO_ERROR("Unable to allocate memory for ALLEGRO_AUDIO_RECORDER fragments\n");
return false;
}
r->fragment_size = r->samples * r->sample_size;
for (i = 0; i < fragment_count; ++i) {
r->fragments[i] = al_malloc(r->fragment_size);
if (!r->fragments[i]) {
size_t j;
for (j = 0; j < i; ++j) {
al_free(r->fragments[j]);
}
al_free(r->fragments);
ALLEGRO_ERROR("Unable to allocate memory for ALLEGRO_AUDIO_RECORDER fragments\n");
return false;
}
}
if (_al_kcm_driver->allocate_recorder(r)) {
ALLEGRO_ERROR("Failed to allocate recorder from driver\n");
return false;
}
r->is_recording = false;
r->mutex = al_create_mutex();
r->cond = al_create_cond();
al_init_user_event_source(&r->source);
if (r->thread) {
/* the driver should have created a thread */
al_start_thread(r->thread);
}
return r;
};
/* Function: al_start_audio_recorder
*/
bool al_start_audio_recorder(ALLEGRO_AUDIO_RECORDER *r)
{
ALLEGRO_ASSERT(r);
al_lock_mutex(r->mutex);
r->is_recording = true;
al_signal_cond(r->cond);
al_unlock_mutex(r->mutex);
return true;
}
/* Function: al_stop_audio_recorder
*/
void al_stop_audio_recorder(ALLEGRO_AUDIO_RECORDER *r)
{
al_lock_mutex(r->mutex);
if (r->is_recording) {
r->is_recording = false;
al_signal_cond(r->cond);
}
al_unlock_mutex(r->mutex);
}
/* Function: al_is_audio_recorder_recording
*/
bool al_is_audio_recorder_recording(ALLEGRO_AUDIO_RECORDER *r)
{
bool is_recording;
al_lock_mutex(r->mutex);
is_recording = r->is_recording;
al_unlock_mutex(r->mutex);
return is_recording;
}
/* Function: al_get_audio_recorder_event
*/
ALLEGRO_AUDIO_RECORDER_EVENT *al_get_audio_recorder_event(ALLEGRO_EVENT *event)
{
ASSERT(event->any.type == ALLEGRO_EVENT_AUDIO_RECORDER_FRAGMENT);
return (ALLEGRO_AUDIO_RECORDER_EVENT *) event;
}
/* Function: al_get_audio_recorder_event_source
*/
ALLEGRO_EVENT_SOURCE *al_get_audio_recorder_event_source(ALLEGRO_AUDIO_RECORDER *r)
{
return &r->source;
}
/* Function: al_destroy_audio_recorder
*/
void al_destroy_audio_recorder(ALLEGRO_AUDIO_RECORDER *r)
{
size_t i;
if (!r)
return;
if (r->thread) {
al_set_thread_should_stop(r->thread);
al_lock_mutex(r->mutex);
r->is_recording = false;
al_signal_cond(r->cond);
al_unlock_mutex(r->mutex);
al_join_thread(r->thread, NULL);
al_destroy_thread(r->thread);
}
if (_al_kcm_driver->deallocate_recorder) {
_al_kcm_driver->deallocate_recorder(r);
}
al_destroy_user_event_source(&r->source);
al_destroy_mutex(r->mutex);
al_destroy_cond(r->cond);
for (i = 0; i < r->fragment_count; ++i) {
al_free(r->fragments[i]);
}
al_free(r->fragments);
al_free(r);
}