Added a MEM_freeN call to sound_free_sound() to release bSound->stream
[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         /* this crashes blender, PLEASE fix! */
32         // still crashes :(
33         //if (sound) {
34         //      sound_set_sample(sound, NULL);
35         //}
36         if (sound->stream) MEM_freeN(sound->stream);
37 }
38
39 void sound_free_sample(bSample *sample)
40 {
41         if (sample) {   
42                 if (sample->data != &sample->fakedata[0] && sample->data != NULL) {
43                         MEM_freeN(sample->data);
44                         sample->data = &sample->fakedata[0];
45                 }
46                 
47                 if (sample->packedfile) {
48                         freePackedFile(sample->packedfile);  //FIXME: crashes sometimes 
49                         sample->packedfile = NULL;
50                 }
51                 
52                 if (sample->alindex != SAMPLE_INVALID) {
53 //                      AUD_free_sample(sample->snd_sample);
54                         sample->alindex = SAMPLE_INVALID;
55                 }
56
57                 sample->type = SAMPLE_INVALID;
58         }
59 }
60
61
62
63 void sound_set_packedfile(bSample *sample, PackedFile *pf)
64 {
65         bSound *sound;
66         
67         if (sample) {
68                 sample->packedfile = pf;
69                 sound = G.main->sound.first;
70                 while (sound) {
71                         if (sound->sample == sample) {
72                                 sound->newpackedfile = pf;
73                                 if (pf == NULL) {
74                                         strcpy(sound->name, sample->name);
75                                 }
76                         }
77                         sound = sound->id.next;
78                 }
79         }
80 }
81
82 PackedFile* sound_find_packedfile(bSound *sound) 
83 {
84         bSound *search;
85         PackedFile *pf = NULL;
86         char soundname[FILE_MAXDIR + FILE_MAXFILE], searchname[FILE_MAXDIR + FILE_MAXFILE];
87         
88         // convert sound->name to abolute filename
89         strcpy(soundname, sound->name);
90         BLI_convertstringcode(soundname, G.sce, G.scene->r.cfra);
91         
92         search = G.main->sound.first;
93         while (search) {
94                 if (search->sample && search->sample->packedfile) {
95                         strcpy(searchname, search->sample->name);
96                         BLI_convertstringcode(searchname, G.sce, G.scene->r.cfra);
97                         
98                         if (BLI_streq(searchname, soundname)) {
99                                 pf = search->sample->packedfile;
100                                 break;
101                         }
102                 } 
103                 
104                 if (search->newpackedfile) {
105                         strcpy(searchname, search->name);
106                         BLI_convertstringcode(searchname, G.sce, G.scene->r.cfra);
107                         if (BLI_streq(searchname, soundname)) {
108                                 pf = search->newpackedfile;
109                                 break;
110                         }
111                 }
112                 search = search->id.next;
113         }
114         
115         return (pf);
116 }