4 * Copyright (C) 2009 Adam Williams <broadcast at earthling dot net>
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.
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.
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
24 // An object which contains samples
27 #include "bcresources.h"
28 #include "bcwindowbase.h"
40 // Allocate number of samples
41 Samples::Samples(int samples)
47 Samples::Samples(Samples *src)
50 int src_sz = src->get_allocated();
52 share(src->get_shmid());
53 set_allocated(src_sz);
59 set_offset(src->get_offset());
69 BC_Resources *resources = BC_WindowBase::get_resources();
70 use_shm = !resources || !resources->use_shm ? 0 : 1;
77 void Samples::clear_objects()
92 void Samples::share(int shmid)
103 data = (double*)shmat(shmid, NULL, 0);
107 void Samples::share(double *buffer)
123 void Samples::allocate(int samples, int use_shm)
125 if( !this->use_shm ) use_shm = 0;
127 this->allocated >= samples &&
128 this->use_shm == use_shm ) return;
137 this->use_shm = use_shm;
140 shmid = shmget(IPC_PRIVATE,
141 (samples + 1) * sizeof(double),
143 data = (double*)shmat(shmid, NULL, 0);
144 // This causes it to automatically delete when the program exits.
145 shmctl(shmid, IPC_RMID, 0);
149 data = new double[samples];
152 this->allocated = samples;
156 double* Samples::get_data()
158 return data + offset;
161 // Get number of samples allocated
162 int Samples::get_allocated()
167 // Set number of samples allocated
168 void Samples::set_allocated(int allocated)
170 this->allocated = allocated;
173 int Samples::get_shmid()
178 void Samples::set_offset(int offset)
180 this->offset = offset;
183 int Samples::get_offset()