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: 2c5b49246fba523e0998c956b2ac8673f95e186f (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
/**
 * 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 "BKE_utildefines.h"
#include "BKE_global.h"
#include "BKE_main.h"
#include "BKE_sound.h"
#include "BKE_packedFile.h"

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

ListBase _samples = {0,0}, *samples = &_samples;

void sound_free_sound(bSound *sound)
{
	/* when sounds have been loaded, but not played, the packedfile was not copied
	   to sample block and not freed otherwise */
	if(sound->sample==NULL) {
		if (sound->newpackedfile) {
			freePackedFile(sound->newpackedfile); 
			sound->newpackedfile = NULL;
		}
	}
	if (sound->stream) free(sound->stream);
}

void sound_free_sample(bSample *sample)
{
	if (sample) {	
		if (sample->data != &sample->fakedata[0] && sample->data != NULL) {
			MEM_freeN(sample->data);
			sample->data = &sample->fakedata[0];
		}
		
		if (sample->packedfile) {
			freePackedFile(sample->packedfile);  //FIXME: crashes sometimes 
			sample->packedfile = NULL;
		}
		
		if (sample->alindex != SAMPLE_INVALID) {
//			AUD_free_sample(sample->snd_sample);
			sample->alindex = SAMPLE_INVALID;
		}

		sample->type = SAMPLE_INVALID;
	}
}

/* this is called after file reading or undos */
void sound_free_all_samples(void)
{
	bSample *sample;
	bSound *sound;
	
	/* ensure no sample pointers exist, and check packedfile */
	for(sound= G.main->sound.first; sound; sound= sound->id.next) {
		if(sound->sample && sound->sample->packedfile==sound->newpackedfile)
			sound->newpackedfile= NULL;
		sound->sample= NULL;
	}
	
	/* now free samples */
	for(sample= samples->first; sample; sample= sample->id.next)
		sound_free_sample(sample);
	BLI_freelistN(samples);
	
}

void sound_set_packedfile(bSample *sample, PackedFile *pf)
{
	bSound *sound;
	
	if (sample) {
		sample->packedfile = pf;
		sound = G.main->sound.first;
		while (sound) {
			if (sound->sample == sample) {
				sound->newpackedfile = pf;
				if (pf == NULL) {
					strcpy(sound->name, sample->name);
				}
			}
			sound = sound->id.next;
		}
	}
}

PackedFile* sound_find_packedfile(bSound *sound) 
{
	bSound *search;
	PackedFile *pf = NULL;
	char soundname[FILE_MAXDIR + FILE_MAXFILE], searchname[FILE_MAXDIR + FILE_MAXFILE];
	
	// convert sound->name to abolute filename
	strcpy(soundname, sound->name);
	BLI_convertstringcode(soundname, G.sce);
	
	search = G.main->sound.first;
	while (search) {
		if (search->sample && search->sample->packedfile) {
			strcpy(searchname, search->sample->name);
			BLI_convertstringcode(searchname, G.sce);
			
			if (BLI_streq(searchname, soundname)) {
				pf = search->sample->packedfile;
				break;
			}
		} 
		
		if (search->newpackedfile) {
			strcpy(searchname, search->name);
			BLI_convertstringcode(searchname, G.sce);
			if (BLI_streq(searchname, soundname)) {
				pf = search->newpackedfile;
				break;
			}
		}
		search = search->id.next;
	}
	
	return (pf);
}