When Sound blocks are loaded, they can have packedfiles. If a Sound isn't
[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
9 #include "MEM_guardedalloc.h"
10
11 #include "BLI_blenlib.h"
12
13 #include "DNA_scene_types.h"
14 #include "DNA_sound_types.h"
15 #include "DNA_packedFile_types.h"
16
17 #include "BKE_utildefines.h"
18 #include "BKE_global.h"
19 #include "BKE_main.h"
20 #include "BKE_sound.h"
21 #include "BKE_packedFile.h"
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 ListBase _samples = {0,0}, *samples = &_samples;
28
29 void sound_free_sound(bSound *sound)
30 {
31         /* when sounds have been loaded, but not played, the packedfile was not copied
32            to sample block and not freed otherwise */
33         if(sound->sample==NULL) {
34                 if (sound->newpackedfile) {
35                         freePackedFile(sound->newpackedfile); 
36                         sound->newpackedfile = NULL;
37                 }
38         }
39         if (sound->stream) MEM_freeN(sound->stream);
40 }
41
42 void sound_free_sample(bSample *sample)
43 {
44         if (sample) {   
45                 if (sample->data != &sample->fakedata[0] && sample->data != NULL) {
46                         MEM_freeN(sample->data);
47                         sample->data = &sample->fakedata[0];
48                 }
49                 
50                 if (sample->packedfile) {
51                         freePackedFile(sample->packedfile);  //FIXME: crashes sometimes 
52                         sample->packedfile = NULL;
53                 }
54                 
55                 if (sample->alindex != SAMPLE_INVALID) {
56 //                      AUD_free_sample(sample->snd_sample);
57                         sample->alindex = SAMPLE_INVALID;
58                 }
59
60                 sample->type = SAMPLE_INVALID;
61         }
62 }
63
64
65
66 void sound_set_packedfile(bSample *sample, PackedFile *pf)
67 {
68         bSound *sound;
69         
70         if (sample) {
71                 sample->packedfile = pf;
72                 sound = G.main->sound.first;
73                 while (sound) {
74                         if (sound->sample == sample) {
75                                 sound->newpackedfile = pf;
76                                 if (pf == NULL) {
77                                         strcpy(sound->name, sample->name);
78                                 }
79                         }
80                         sound = sound->id.next;
81                 }
82         }
83 }
84
85 PackedFile* sound_find_packedfile(bSound *sound) 
86 {
87         bSound *search;
88         PackedFile *pf = NULL;
89         char soundname[FILE_MAXDIR + FILE_MAXFILE], searchname[FILE_MAXDIR + FILE_MAXFILE];
90         
91         // convert sound->name to abolute filename
92         strcpy(soundname, sound->name);
93         BLI_convertstringcode(soundname, G.sce, G.scene->r.cfra);
94         
95         search = G.main->sound.first;
96         while (search) {
97                 if (search->sample && search->sample->packedfile) {
98                         strcpy(searchname, search->sample->name);
99                         BLI_convertstringcode(searchname, G.sce, G.scene->r.cfra);
100                         
101                         if (BLI_streq(searchname, soundname)) {
102                                 pf = search->sample->packedfile;
103                                 break;
104                         }
105                 } 
106                 
107                 if (search->newpackedfile) {
108                         strcpy(searchname, search->name);
109                         BLI_convertstringcode(searchname, G.sce, G.scene->r.cfra);
110                         if (BLI_streq(searchname, soundname)) {
111                                 pf = search->newpackedfile;
112                                 break;
113                         }
114                 }
115                 search = search->id.next;
116         }
117         
118         return (pf);
119 }