1 /*************************************************************************/
2 /*  audio_driver_bb10.cpp                                                */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md)    */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 #include "audio_driver_bb10.h"
31 
32 #include <errno.h>
33 
init()34 Error AudioDriverBB10::init() {
35 	return init(NULL);
36 };
37 
init(const char * p_name)38 Error AudioDriverBB10::init(const char *p_name) {
39 
40 	active = false;
41 	thread_exited = false;
42 	exit_thread = false;
43 	pcm_open = false;
44 	samples_in = NULL;
45 	samples_out = NULL;
46 
47 	mix_rate = 44100;
48 	output_format = OUTPUT_STEREO;
49 
50 	char *dev_name;
51 	if (p_name == NULL) {
52 		dev_name = "pcmPreferred";
53 	} else {
54 		dev_name = (char *)p_name;
55 	}
56 	printf("******** reconnecting to device %s\n", dev_name);
57 	int ret = snd_pcm_open_name(&pcm_handle, dev_name, SND_PCM_OPEN_PLAYBACK);
58 	ERR_FAIL_COND_V(ret < 0, FAILED);
59 	pcm_open = true;
60 
61 	snd_pcm_channel_info_t cinfo;
62 	zeromem(&cinfo, sizeof(cinfo));
63 	cinfo.channel = SND_PCM_CHANNEL_PLAYBACK;
64 	snd_pcm_plugin_info(pcm_handle, &cinfo);
65 
66 	printf("rates %i, %i, %i, %i, %i\n", cinfo.rates, cinfo.rates & SND_PCM_RATE_44100, cinfo.rates & SND_PCM_RATE_32000, cinfo.rates & SND_PCM_RATE_22050, cinfo.max_rate);
67 
68 	mix_rate = cinfo.max_rate;
69 
70 	printf("formats %i, %i, %i\n", cinfo.formats, cinfo.formats & SND_PCM_FMT_S16_BE, cinfo.formats & SND_PCM_FMT_S16_LE);
71 	ERR_FAIL_COND_V(!(cinfo.formats & SND_PCM_FMT_S16_LE), FAILED);
72 
73 	printf("voices %i\n", cinfo.max_voices);
74 	output_format = cinfo.max_voices >= 2 ? OUTPUT_STEREO : OUTPUT_MONO;
75 
76 	snd_pcm_channel_params_t cp;
77 	zeromem(&cp, sizeof(cp));
78 	cp.mode = SND_PCM_MODE_BLOCK;
79 	cp.channel = SND_PCM_CHANNEL_PLAYBACK;
80 	cp.start_mode = SND_PCM_START_DATA;
81 	cp.stop_mode = SND_PCM_STOP_STOP;
82 	//cp.buf.block.frag_size = cinfo.max_fragment_size;
83 	cp.buf.block.frag_size = 512;
84 	cp.buf.block.frags_max = 1;
85 	cp.buf.block.frags_min = 1;
86 	cp.format.interleave = 1;
87 	cp.format.rate = mix_rate;
88 	cp.format.voices = output_format == OUTPUT_MONO ? 1 : 2;
89 	cp.format.format = SND_PCM_SFMT_S16_LE;
90 
91 	ret = snd_pcm_plugin_params(pcm_handle, &cp);
92 	printf("ret is %i, %i\n", ret, cp.why_failed);
93 	ERR_FAIL_COND_V(ret < 0, FAILED);
94 
95 	ret = snd_pcm_plugin_prepare(pcm_handle, SND_PCM_CHANNEL_PLAYBACK);
96 	ERR_FAIL_COND_V(ret < 0, FAILED);
97 
98 	snd_mixer_group_t group;
99 	zeromem(&group, sizeof(group));
100 	snd_pcm_channel_setup_t setup;
101 	zeromem(&setup, sizeof(setup));
102 	setup.channel = SND_PCM_CHANNEL_PLAYBACK;
103 	setup.mode = SND_PCM_MODE_BLOCK;
104 	setup.mixer_gid = &group.gid;
105 	ret = snd_pcm_plugin_setup(pcm_handle, &setup);
106 	ERR_FAIL_COND_V(ret < 0, FAILED);
107 
108 	pcm_frag_size = setup.buf.block.frag_size;
109 	pcm_max_frags = 1;
110 
111 	sample_buf_count = pcm_frag_size * pcm_max_frags / 2;
112 	printf("sample count %i, %i, %i\n", sample_buf_count, pcm_frag_size, pcm_max_frags);
113 	samples_in = memnew_arr(int32_t, sample_buf_count);
114 	samples_out = memnew_arr(int16_t, sample_buf_count);
115 
116 	thread = Thread::create(AudioDriverBB10::thread_func, this);
117 
118 	return OK;
119 };
120 
thread_func(void * p_udata)121 void AudioDriverBB10::thread_func(void *p_udata) {
122 
123 	AudioDriverBB10 *ad = (AudioDriverBB10 *)p_udata;
124 
125 	int channels = (ad->output_format == OUTPUT_MONO ? 1 : 2);
126 	int frame_count = ad->sample_buf_count / channels;
127 	int bytes_out = frame_count * channels * 2;
128 
129 	while (!ad->exit_thread) {
130 
131 		if (!ad->active) {
132 
133 			for (int i = 0; i < ad->sample_buf_count; i++) {
134 
135 				ad->samples_out[i] = 0;
136 			};
137 		} else {
138 
139 			ad->lock();
140 
141 			ad->audio_server_process(frame_count, ad->samples_in);
142 
143 			ad->unlock();
144 
145 			for (int i = 0; i < frame_count * channels; i++) {
146 
147 				ad->samples_out[i] = ad->samples_in[i] >> 16;
148 			}
149 		};
150 
151 		int todo = bytes_out;
152 		int total = 0;
153 
154 		while (todo) {
155 
156 			uint8_t *src = (uint8_t *)ad->samples_out;
157 			int wrote = snd_pcm_plugin_write(ad->pcm_handle, (void *)(src + total), todo);
158 			if (wrote < 0) {
159 				// error?
160 				break;
161 			};
162 			total += wrote;
163 			todo -= wrote;
164 			if (wrote < todo) {
165 				if (ad->thread_exited) {
166 					break;
167 				};
168 				printf("pcm_write underrun %i, errno %i\n", (int)ad->thread_exited, errno);
169 				snd_pcm_channel_status_t status;
170 				zeromem(&status, sizeof(status));
171 				// put in non-blocking mode
172 				snd_pcm_nonblock_mode(ad->pcm_handle, 1);
173 				status.channel = SND_PCM_CHANNEL_PLAYBACK;
174 				int ret = snd_pcm_plugin_status(ad->pcm_handle, &status);
175 				//printf("status return %i, %i, %i, %i, %i\n", ret, errno, status.status, SND_PCM_STATUS_READY, SND_PCM_STATUS_UNDERRUN);
176 				snd_pcm_nonblock_mode(ad->pcm_handle, 0);
177 				if (ret < 0) {
178 					break;
179 				};
180 				if (status.status == SND_PCM_STATUS_READY ||
181 						status.status == SND_PCM_STATUS_UNDERRUN) {
182 					snd_pcm_plugin_prepare(ad->pcm_handle, SND_PCM_CHANNEL_PLAYBACK);
183 				} else {
184 					break;
185 				};
186 			};
187 		};
188 	};
189 
190 	snd_pcm_plugin_flush(ad->pcm_handle, SND_PCM_CHANNEL_PLAYBACK);
191 
192 	ad->thread_exited = true;
193 	printf("**************** audio thread exit\n");
194 };
195 
start()196 void AudioDriverBB10::start() {
197 
198 	active = true;
199 };
200 
get_mix_rate() const201 int AudioDriverBB10::get_mix_rate() const {
202 
203 	return mix_rate;
204 };
205 
get_output_format() const206 AudioDriverSW::OutputFormat AudioDriverBB10::get_output_format() const {
207 
208 	return output_format;
209 };
lock()210 void AudioDriverBB10::lock() {
211 
212 	if (!thread)
213 		return;
214 	mutex->lock();
215 };
unlock()216 void AudioDriverBB10::unlock() {
217 
218 	if (!thread)
219 		return;
220 	mutex->unlock();
221 };
222 
finish()223 void AudioDriverBB10::finish() {
224 
225 	if (!thread)
226 		return;
227 
228 	exit_thread = true;
229 	Thread::wait_to_finish(thread);
230 
231 	if (pcm_open)
232 		snd_pcm_close(pcm_handle);
233 
234 	if (samples_in) {
235 		memdelete_arr(samples_in);
236 		memdelete_arr(samples_out);
237 	};
238 
239 	memdelete(thread);
240 	thread = NULL;
241 };
242 
AudioDriverBB10()243 AudioDriverBB10::AudioDriverBB10() {
244 
245 	mutex = Mutex::create();
246 };
247 
~AudioDriverBB10()248 AudioDriverBB10::~AudioDriverBB10() {
249 
250 	memdelete(mutex);
251 	mutex = NULL;
252 };
253