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