Welcome to mirror list, hosted at ThFree Co, Russian Federation.

sound.c « intern « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2d5d8dad7a8bc8d97941d3a2f2e98050329d9bd6 (plain)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/**
 * sound.c (mar-2001 nzc)
 *
 * $Id$
 */

#include <string.h>
#include <stdlib.h>

#include "MEM_guardedalloc.h"

#include "BLI_blenlib.h"

#include "DNA_scene_types.h"
#include "DNA_sound_types.h"
#include "DNA_packedFile_types.h"
#include "DNA_screen_types.h"
#include "DNA_userdef_types.h"

#include "AUD_C-API.h"

#include "BKE_utildefines.h"
#include "BKE_global.h"
#include "BKE_main.h"
#include "BKE_sound.h"
#include "BKE_context.h"
#include "BKE_library.h"
#include "BKE_packedFile.h"

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

void sound_init()
{
	AUD_Specs specs;
	int device, buffersize;

	device = U.audiodevice;
	buffersize = U.mixbufsize;
	specs.channels = U.audiochannels;
	specs.format = U.audioformat;
	specs.rate = U.audiorate;

	if(buffersize < 128)
		buffersize = AUD_DEFAULT_BUFFER_SIZE;

	if(specs.rate < AUD_RATE_8000)
		specs.rate = AUD_RATE_44100;

	if(specs.format <= AUD_FORMAT_INVALID)
		specs.format = AUD_FORMAT_S16;

	if(specs.channels <= AUD_CHANNELS_INVALID)
		specs.channels = AUD_CHANNELS_STEREO;

	if(!AUD_init(device, specs, buffersize))
		AUD_init(AUD_NULL_DEVICE, specs, buffersize);
}

void sound_exit()
{
	AUD_exit();
}

struct bSound* sound_new_file(struct Main *main, char* filename)
{
	bSound* sound = NULL;

	char str[FILE_MAX];
	int len;

	strcpy(str, filename);
	BLI_convertstringcode(str, main->name);

	len = strlen(filename);
	while(len > 0 && filename[len-1] != '/' && filename[len-1] != '\\')
		len--;

	sound = alloc_libblock(&main->sound, ID_SO, filename+len);
	strcpy(sound->name, filename);
// XXX unused currently	sound->type = SOUND_TYPE_FILE;

	sound_load(main, sound);

	if(!sound->handle)
	{
		free_libblock(&main->sound, sound);
		sound = NULL;
	}

	return sound;
}

// XXX unused currently
#if 0
struct bSound* sound_new_buffer(struct bContext *C, struct bSound *source)
{
	bSound* sound = NULL;

	char name[25];
	strcpy(name, "buf_");
	strcpy(name + 4, source->id.name);

	sound = alloc_libblock(&CTX_data_main(C)->sound, ID_SO, name);

	sound->child_sound = source;
	sound->type = SOUND_TYPE_BUFFER;

	sound_load(CTX_data_main(C), sound);

	if(!sound->handle)
	{
		free_libblock(&CTX_data_main(C)->sound, sound);
		sound = NULL;
	}

	return sound;
}

struct bSound* sound_new_limiter(struct bContext *C, struct bSound *source, float start, float end)
{
	bSound* sound = NULL;

	char name[25];
	strcpy(name, "lim_");
	strcpy(name + 4, source->id.name);

	sound = alloc_libblock(&CTX_data_main(C)->sound, ID_SO, name);

	sound->child_sound = source;
	sound->start = start;
	sound->end = end;
	sound->type = SOUND_TYPE_LIMITER;

	sound_load(CTX_data_main(C), sound);

	if(!sound->handle)
	{
		free_libblock(&CTX_data_main(C)->sound, sound);
		sound = NULL;
	}

	return sound;
}
#endif

void sound_delete(struct bContext *C, struct bSound* sound)
{
	if(sound)
	{
		sound_free(sound);

		sound_unlink(C, sound);

		free_libblock(&CTX_data_main(C)->sound, sound);
	}
}

void sound_cache(struct bSound* sound, int ignore)
{
	if(sound->cache && !ignore)
		AUD_unload(sound->cache);

	sound->cache = AUD_bufferSound(sound->handle);
	sound->changed++;
}

void sound_delete_cache(struct bSound* sound)
{
	if(sound->cache)
	{
		AUD_unload(sound->cache);
		sound->cache = NULL;
	}
}

void sound_load(struct Main *main, struct bSound* sound)
{
	if(sound)
	{
		if(sound->handle)
		{
			AUD_unload(sound->handle);
			sound->handle = NULL;
		}

// XXX unused currently
#if 0
		switch(sound->type)
		{
		case SOUND_TYPE_FILE:
#endif
		{
			char fullpath[FILE_MAX];
			char *path;

			/* load sound */
			PackedFile* pf = sound->packedfile;

			/* dont modify soundact->sound->name, only change a copy */
			BLI_strncpy(fullpath, sound->name, sizeof(fullpath));

			if(sound->id.lib)
				path = sound->id.lib->filename;
			else
				path = main ? main->name : G.sce;

			BLI_convertstringcode(fullpath, path);

			/* but we need a packed file then */
			if (pf)
				sound->handle = AUD_loadBuffer((unsigned char*) pf->data, pf->size);
			/* or else load it from disk */
			else
				sound->handle = AUD_load(fullpath);
		} // XXX
// XXX unused currently
#if 0
			break;
		}
		case SOUND_TYPE_BUFFER:
			if(sound->child_sound && sound->child_sound->handle)
				sound->handle = AUD_bufferSound(sound->child_sound->handle);
			break;
		case SOUND_TYPE_LIMITER:
			if(sound->child_sound && sound->child_sound->handle)
				sound->handle = AUD_limitSound(sound->child_sound, sound->start, sound->end);
			break;
		}
#endif
		sound->changed++;
	}
}

void sound_free(struct bSound* sound)
{
	if (sound->packedfile)
	{
		freePackedFile(sound->packedfile);
		sound->packedfile = NULL;
	}

	if(sound->handle)
	{
		AUD_unload(sound->handle);
		sound->handle = NULL;
	}
}

void sound_unlink(struct bContext *C, struct bSound* sound)
{
	Scene *scene;
	SoundHandle *handle;

// XXX unused currently
#if 0
	bSound *snd;
	for(snd = CTX_data_main(C)->sound.first; snd; snd = snd->id.next)
	{
		if(snd->child_sound == sound)
		{
			snd->child_sound = NULL;
			if(snd->handle)
			{
				AUD_unload(sound->handle);
				snd->handle = NULL;
			}

			sound_unlink(C, snd);
		}
	}
#endif

	for(scene = CTX_data_main(C)->scene.first; scene; scene = scene->id.next)
	{
		for(handle = scene->sound_handles.first; handle; handle = handle->next)
		{
			if(handle->source == sound)
			{
				handle->source = NULL;
				if(handle->handle)
					AUD_stop(handle->handle);
			}
		}
	}
}

struct SoundHandle* sound_new_handle(struct Scene *scene, struct bSound* sound, int startframe, int endframe, int frameskip)
{
	ListBase* handles = &scene->sound_handles;

	SoundHandle* handle = MEM_callocN(sizeof(SoundHandle), "sound_handle");
	handle->source = sound;
	handle->startframe = startframe;
	handle->endframe = endframe;
	handle->frameskip = frameskip;
	handle->state = AUD_STATUS_INVALID;
	handle->volume = 1.0f;

	BLI_addtail(handles, handle);

	return handle;
}

void sound_delete_handle(struct Scene *scene, struct SoundHandle *handle)
{
	if(handle == NULL)
		return;

	if(handle->handle)
		AUD_stop(handle->handle);

	BLI_freelinkN(&scene->sound_handles, handle);
}

void sound_stop_all(struct bContext *C)
{
	SoundHandle *handle;

	for(handle = CTX_data_scene(C)->sound_handles.first; handle; handle = handle->next)
	{
		if(handle->state == AUD_STATUS_PLAYING)
		{
			AUD_pause(handle->handle);
			handle->state = AUD_STATUS_PAUSED;
		}
	}
}

void sound_update_playing(struct bContext *C)
{
	SoundHandle *handle;
	Scene* scene = CTX_data_scene(C);
	int cfra = CFRA;
	float fps = FPS;
	int action;

	AUD_lock();

	for(handle = scene->sound_handles.first; handle; handle = handle->next)
	{
		if(cfra < handle->startframe || cfra >= handle->endframe || handle->mute)
		{
			if(handle->state == AUD_STATUS_PLAYING)
			{
				AUD_pause(handle->handle);
				handle->state = AUD_STATUS_PAUSED;
			}
		}
		else
		{
			action = 0;

			if(handle->changed != handle->source->changed)
			{
				handle->changed = handle->source->changed;
				action = 3;
				if(handle->state != AUD_STATUS_INVALID)
				{
					AUD_stop(handle->handle);
					handle->state = AUD_STATUS_INVALID;
				}
			}
			else
			{
				if(handle->state != AUD_STATUS_PLAYING)
					action = 3;
				else
				{
					handle->state = AUD_getStatus(handle->handle);
					if(handle->state != AUD_STATUS_PLAYING)
						action = 3;
					else
					{
						float diff = AUD_getPosition(handle->handle) * fps - cfra + handle->startframe;
						if(diff < 0.0)
							diff = -diff;
						if(diff > FPS/2.0)
						{
							action = 2;
						}
					}
				}
			}

			if(action & 1)
			{
				if(handle->state == AUD_STATUS_INVALID)
				{
					if(handle->source && handle->source->handle)
					{
						AUD_Sound* limiter = AUD_limitSound(handle->source->cache ? handle->source->cache : handle->source->handle, handle->frameskip / fps, (handle->frameskip + handle->endframe - handle->startframe)/fps);
						handle->handle = AUD_play(limiter, 1);
						AUD_unload(limiter);
						if(handle->handle)
							handle->state = AUD_STATUS_PLAYING;
						if(cfra == handle->startframe)
							action &= ~2;
					}
				}
				else
					if(AUD_resume(handle->handle))
						handle->state = AUD_STATUS_PLAYING;
					else
						handle->state = AUD_STATUS_INVALID;
			}

			if(action & 2)
				AUD_seek(handle->handle, (cfra - handle->startframe) / fps);
		}
	}

	AUD_unlock();
}

void sound_scrub(struct bContext *C)
{
	SoundHandle *handle;
	Scene* scene = CTX_data_scene(C);
	int cfra = CFRA;
	float fps = FPS;

	if(scene->r.audio.flag & AUDIO_SCRUB && !CTX_wm_screen(C)->animtimer)
	{
		AUD_lock();

		for(handle = scene->sound_handles.first; handle; handle = handle->next)
		{
			if(cfra >= handle->startframe && cfra < handle->endframe && !handle->mute)
			{
				if(handle->source && handle->source->handle)
				{
					int frameskip = handle->frameskip + cfra - handle->startframe;
					AUD_Sound* limiter = AUD_limitSound(handle->source->cache ? handle->source->cache : handle->source->handle, frameskip / fps, (frameskip + 1)/fps);
					AUD_play(limiter, 0);
					AUD_unload(limiter);
				}
			}
		}

		AUD_unlock();
	}
}

AUD_Device* sound_mixdown(struct Scene *scene, AUD_Specs specs, int start, int end)
{
	AUD_Device* mixdown = AUD_openReadDevice(specs);
	SoundHandle *handle;
	float fps = FPS;
	AUD_Sound *limiter, *delayer;
	int frameskip, s, e;

	end++;

	for(handle = scene->sound_handles.first; handle; handle = handle->next)
	{
		if(start < handle->endframe && end > handle->startframe && !handle->mute && handle->source && handle->source->handle)
		{
			frameskip = handle->frameskip;
			s = handle->startframe - start;
			e = handle->frameskip + AUD_MIN(handle->endframe, end) - handle->startframe;

			if(s < 0)
			{
				frameskip -= s;
				s = 0;
			}

			limiter = AUD_limitSound(handle->source->handle, frameskip / fps, e / fps);
			delayer = AUD_delaySound(limiter, s / fps);

			AUD_playDevice(mixdown, delayer);

			AUD_unload(delayer);
			AUD_unload(limiter);
		}
	}

	return mixdown;
}