edits ontop of Alex's patch from r41292.
[blender.git] / source / blender / blenkernel / intern / speaker.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Jörg Müller.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/speaker.c
29  *  \ingroup bke
30  */
31
32 #include "DNA_object_types.h"
33 #include "DNA_sound_types.h"
34 #include "DNA_speaker_types.h"
35
36 #include "BLI_math.h"
37
38 #include "BKE_animsys.h"
39 #include "BKE_global.h"
40 #include "BKE_library.h"
41 #include "BKE_main.h"
42 #include "BKE_speaker.h"
43
44 void *add_speaker(const char *name)
45 {
46         Speaker *spk;
47
48         spk=  alloc_libblock(&G.main->speaker, ID_SPK, name);
49
50         spk->attenuation = 1.0f;
51         spk->cone_angle_inner = 360.0f;
52         spk->cone_angle_outer = 360.0f;
53         spk->cone_volume_outer = 1.0f;
54         spk->distance_max = FLT_MAX;
55         spk->distance_reference = 1.0f;
56         spk->flag = 0;
57         spk->pitch = 1.0f;
58         spk->sound = NULL;
59         spk->volume = 1.0f;
60         spk->volume_max = 1.0f;
61         spk->volume_min = 0.0f;
62
63         return spk;
64 }
65
66 Speaker *copy_speaker(Speaker *spk)
67 {
68         Speaker *spkn;
69
70         spkn= copy_libblock(spk);
71         if(spkn->sound)
72                 spkn->sound->id.us++;
73
74         return spkn;
75 }
76
77 void make_local_speaker(Speaker *spk)
78 {
79         Main *bmain= G.main;
80         Object *ob;
81         int local=0, lib=0;
82
83         /* - only lib users: do nothing
84                 * - only local users: set flag
85                 * - mixed: make copy
86                 */
87
88         if(spk->id.lib==NULL) return;
89         if(spk->id.us==1) {
90                 id_clear_lib_data(bmain, (ID *)spk);
91                 return;
92         }
93
94         ob= bmain->object.first;
95         while(ob) {
96                 if(ob->data==spk) {
97                         if(ob->id.lib) lib= 1;
98                         else local= 1;
99                 }
100                 ob= ob->id.next;
101         }
102
103         if(local && lib==0) {
104                 id_clear_lib_data(bmain, (ID *)spk);
105         }
106         else if(local && lib) {
107                 Speaker *spkn= copy_speaker(spk);
108                 spkn->id.us= 0;
109
110                 ob= bmain->object.first;
111                 while(ob) {
112                         if(ob->data==spk) {
113
114                                 if(ob->id.lib==NULL) {
115                                         ob->data= spkn;
116                                         spkn->id.us++;
117                                         spk->id.us--;
118                                 }
119                         }
120                         ob= ob->id.next;
121                 }
122         }
123 }
124
125 void free_speaker(Speaker *spk)
126 {
127         if(spk->sound)
128                 spk->sound->id.us--;
129
130         BKE_free_animdata((ID *)spk);
131 }