5466630f621a05c70bff540ed9405582ee84247c
[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 #include "BLI_utildefines.h"
38 #include "BLI_bpath.h"
39
40 #include "BKE_animsys.h"
41 #include "BKE_global.h"
42 #include "BKE_library.h"
43 #include "BKE_main.h"
44 #include "BKE_speaker.h"
45
46 void *add_speaker(const char *name)
47 {
48         Speaker *spk;
49
50         spk=  alloc_libblock(&G.main->speaker, ID_SPK, name);
51
52         spk->attenuation = 1.0f;
53         spk->cone_angle_inner = 360.0f;
54         spk->cone_angle_outer = 360.0f;
55         spk->cone_volume_outer = 1.0f;
56         spk->distance_max = FLT_MAX;
57         spk->distance_reference = 1.0f;
58         spk->flag = 0;
59         spk->pitch = 1.0f;
60         spk->sound = NULL;
61         spk->volume = 1.0f;
62         spk->volume_max = 1.0f;
63         spk->volume_min = 0.0f;
64
65         return spk;
66 }
67
68 Speaker *copy_speaker(Speaker *spk)
69 {
70         Speaker *spkn;
71
72         spkn= copy_libblock(&spk->id);
73         if (spkn->sound)
74                 spkn->sound->id.us++;
75
76         return spkn;
77 }
78
79 void make_local_speaker(Speaker *spk)
80 {
81         Main *bmain= G.main;
82         Object *ob;
83         int is_local= FALSE, is_lib= FALSE;
84
85         /* - only lib users: do nothing
86          * - only local users: set flag
87          * - mixed: make copy
88          */
89
90         if (spk->id.lib==NULL) return;
91         if (spk->id.us==1) {
92                 id_clear_lib_data(bmain, &spk->id);
93                 return;
94         }
95
96         ob= bmain->object.first;
97         while (ob) {
98                 if (ob->data==spk) {
99                         if (ob->id.lib) is_lib= TRUE;
100                         else is_local= TRUE;
101                 }
102                 ob= ob->id.next;
103         }
104
105         if (is_local && is_lib == FALSE) {
106                 id_clear_lib_data(bmain, &spk->id);
107         }
108         else if (is_local && is_lib) {
109                 Speaker *spk_new= copy_speaker(spk);
110                 spk_new->id.us= 0;
111
112                 /* Remap paths of new ID using old library as base. */
113                 BKE_id_lib_local_paths(bmain, spk->id.lib, &spk_new->id);
114
115                 ob= bmain->object.first;
116                 while (ob) {
117                         if (ob->data==spk) {
118
119                                 if (ob->id.lib==NULL) {
120                                         ob->data= spk_new;
121                                         spk_new->id.us++;
122                                         spk->id.us--;
123                                 }
124                         }
125                         ob= ob->id.next;
126                 }
127         }
128 }
129
130 void free_speaker(Speaker *spk)
131 {
132         if (spk->sound)
133                 spk->sound->id.us--;
134
135         BKE_free_animdata((ID *)spk);
136 }