BGE Button types panel, can edit existing buttons but not add new ones yet.
[blender.git] / source / blender / blenkernel / intern / sound.c
1 /**
2  * sound.c (mar-2001 nzc)
3  *      
4  * $Id$
5  */
6
7 #include <string.h>
8 #include <stdlib.h>
9
10 #include "MEM_guardedalloc.h"
11
12 #include "BLI_blenlib.h"
13
14 #include "DNA_scene_types.h"
15 #include "DNA_sound_types.h"
16 #include "DNA_packedFile_types.h"
17
18 #include "BKE_utildefines.h"
19 #include "BKE_global.h"
20 #include "BKE_main.h"
21 #include "BKE_sound.h"
22 #include "BKE_packedFile.h"
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 ListBase _samples = {0,0}, *samples = &_samples;
29
30 void sound_free_sound(bSound *sound)
31 {
32         /* when sounds have been loaded, but not played, the packedfile was not copied
33            to sample block and not freed otherwise */
34         if(sound->sample==NULL) {
35                 if (sound->newpackedfile) {
36                         freePackedFile(sound->newpackedfile); 
37                         sound->newpackedfile = NULL;
38                 }
39         }
40         if (sound->stream) free(sound->stream);
41 }
42
43 void sound_free_sample(bSample *sample)
44 {
45         if (sample) {   
46                 if (sample->data != &sample->fakedata[0] && sample->data != NULL) {
47                         MEM_freeN(sample->data);
48                         sample->data = &sample->fakedata[0];
49                 }
50                 
51                 if (sample->packedfile) {
52                         freePackedFile(sample->packedfile);  //FIXME: crashes sometimes 
53                         sample->packedfile = NULL;
54                 }
55                 
56                 if (sample->alindex != SAMPLE_INVALID) {
57 //                      AUD_free_sample(sample->snd_sample);
58                         sample->alindex = SAMPLE_INVALID;
59                 }
60
61                 sample->type = SAMPLE_INVALID;
62         }
63 }
64
65 /* this is called after file reading or undos */
66 void sound_free_all_samples(void)
67 {
68         bSample *sample;
69         bSound *sound;
70         
71         /* ensure no sample pointers exist, and check packedfile */
72         for(sound= G.main->sound.first; sound; sound= sound->id.next) {
73                 if(sound->sample && sound->sample->packedfile==sound->newpackedfile)
74                         sound->newpackedfile= NULL;
75                 sound->sample= NULL;
76         }
77         
78         /* now free samples */
79         for(sample= samples->first; sample; sample= sample->id.next)
80                 sound_free_sample(sample);
81         BLI_freelistN(samples);
82         
83 }
84
85 void sound_set_packedfile(bSample *sample, PackedFile *pf)
86 {
87         bSound *sound;
88         
89         if (sample) {
90                 sample->packedfile = pf;
91                 sound = G.main->sound.first;
92                 while (sound) {
93                         if (sound->sample == sample) {
94                                 sound->newpackedfile = pf;
95                                 if (pf == NULL) {
96                                         strcpy(sound->name, sample->name);
97                                 }
98                         }
99                         sound = sound->id.next;
100                 }
101         }
102 }
103
104 PackedFile* sound_find_packedfile(bSound *sound) 
105 {
106         bSound *search;
107         PackedFile *pf = NULL;
108         char soundname[FILE_MAXDIR + FILE_MAXFILE], searchname[FILE_MAXDIR + FILE_MAXFILE];
109         
110         // convert sound->name to abolute filename
111         strcpy(soundname, sound->name);
112         BLI_convertstringcode(soundname, G.sce);
113         
114         search = G.main->sound.first;
115         while (search) {
116                 if (search->sample && search->sample->packedfile) {
117                         strcpy(searchname, search->sample->name);
118                         BLI_convertstringcode(searchname, G.sce);
119                         
120                         if (BLI_streq(searchname, soundname)) {
121                                 pf = search->sample->packedfile;
122                                 break;
123                         }
124                 } 
125                 
126                 if (search->newpackedfile) {
127                         strcpy(searchname, search->name);
128                         BLI_convertstringcode(searchname, G.sce);
129                         if (BLI_streq(searchname, soundname)) {
130                                 pf = search->newpackedfile;
131                                 break;
132                         }
133                 }
134                 search = search->id.next;
135         }
136         
137         return (pf);
138 }