Cycles: svn merge -r41225:41232 ^/trunk/blender
[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                 spk->id.lib= NULL;
91                 spk->id.flag= LIB_LOCAL;
92                 new_id(&bmain->speaker, (ID *)spk, NULL);
93                 return;
94         }
95
96         ob= bmain->object.first;
97         while(ob) {
98                 if(ob->data==spk) {
99                         if(ob->id.lib) lib= 1;
100                         else local= 1;
101                 }
102                 ob= ob->id.next;
103         }
104
105         if(local && lib==0) {
106                 spk->id.lib= NULL;
107                 spk->id.flag= LIB_LOCAL;
108                 new_id(&bmain->speaker, (ID *)spk, NULL);
109         }
110         else if(local && lib) {
111                 Speaker *spkn= copy_speaker(spk);
112                 spkn->id.us= 0;
113
114                 ob= bmain->object.first;
115                 while(ob) {
116                         if(ob->data==spk) {
117
118                                 if(ob->id.lib==NULL) {
119                                         ob->data= spkn;
120                                         spkn->id.us++;
121                                         spk->id.us--;
122                                 }
123                         }
124                         ob= ob->id.next;
125                 }
126         }
127 }
128
129 void free_speaker(Speaker *spk)
130 {
131         if(spk->sound)
132                 spk->sound->id.us--;
133
134         BKE_free_animdata((ID *)spk);
135 }