drag edithandle rework, lib x264/x265 update, dvb fixes, batchrender boot_defaults...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / samples.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2009 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22
23
24 // An object which contains samples
25
26
27
28 #include "samples.h"
29 #include <stdio.h>
30 #include <sys/shm.h>
31 #include <unistd.h>
32
33
34 Samples::Samples()
35 {
36         reset();
37 }
38
39 // Allocate number of samples
40 Samples::Samples(int samples)
41 {
42         reset();
43         allocate(samples, 1);
44 }
45
46 Samples::Samples(Samples *src)
47 {
48         reset();
49         share(src->get_shmid());
50         set_allocated(src->get_allocated());
51         set_offset(src->get_offset());
52 }
53
54 Samples::~Samples()
55 {
56         clear_objects();
57 }
58
59 void Samples::reset()
60 {
61         use_shm = 1;
62         shmid = -1;
63         data = 0;
64         allocated = 0;
65         offset = 0;
66 }
67
68 void Samples::clear_objects()
69 {
70         if(use_shm)
71         {
72                 if(data) shmdt(data);
73         }
74         else
75         {
76                 delete [] data;
77         }
78
79         reset();
80 }
81
82
83 void Samples::share(int shmid)
84 {
85         if(data)
86         {
87                 if(use_shm)
88                         shmdt(data);
89                 else
90                         delete [] data;
91         }
92
93         this->use_shm = 1;
94         data = (double*)shmat(shmid, NULL, 0);
95         this->allocated = 0;
96         this->shmid = shmid;
97 }
98
99 void Samples::allocate(int samples, int use_shm)
100 {
101         if(data &&
102                 this->allocated >= samples &&
103                 this->use_shm == use_shm) return;
104
105         if(data)
106         {
107                 if(this->use_shm)
108                         shmdt(data);
109                 else
110                         delete [] data;
111         }
112
113         this->use_shm = use_shm;
114
115         if(use_shm)
116         {
117                 shmid = shmget(IPC_PRIVATE,
118                         (samples + 1) * sizeof(double),
119                         IPC_CREAT | 0777);
120                 data = (double*)shmat(shmid, NULL, 0);
121         // This causes it to automatically delete when the program exits.
122                 shmctl(shmid, IPC_RMID, 0);
123         }
124         else
125         {
126                 shmid = -1;
127                 data = new double[samples];
128         }
129
130
131         this->allocated = samples;
132
133
134
135
136 }
137
138 // Get the buffer
139 double* Samples::get_data()
140 {
141         return data + offset;
142 }
143
144 // Get number of samples allocated
145 int Samples::get_allocated()
146 {
147         return allocated;
148 }
149
150 // Set number of samples allocated
151 void Samples::set_allocated(int allocated)
152 {
153         this->allocated = allocated;
154 }
155
156 int Samples::get_shmid()
157 {
158         return shmid;
159 }
160
161 void Samples::set_offset(int offset)
162 {
163         this->offset = offset;
164 }
165
166 int Samples::get_offset()
167 {
168         return offset;
169 }
170