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