| 1 |
/* |
|---|
| 2 |
* Sweep, a sound wave editor. |
|---|
| 3 |
* |
|---|
| 4 |
* Copyright (C) 2000 Conrad Parker |
|---|
| 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 |
#ifdef HAVE_CONFIG_H |
|---|
| 22 |
# include <config.h> |
|---|
| 23 |
#endif |
|---|
| 24 |
|
|---|
| 25 |
#include <stdio.h> |
|---|
| 26 |
#include <string.h> |
|---|
| 27 |
#include <glib.h> |
|---|
| 28 |
|
|---|
| 29 |
#include <sweep/sweep_types.h> |
|---|
| 30 |
#include <sweep/sweep_typeconvert.h> |
|---|
| 31 |
#include <sweep/sweep_selection.h> |
|---|
| 32 |
#include <sweep/sweep_undo.h> |
|---|
| 33 |
|
|---|
| 34 |
#include "edit.h" |
|---|
| 35 |
#include "format.h" |
|---|
| 36 |
#include "view.h" |
|---|
| 37 |
#include "sample-display.h" |
|---|
| 38 |
#include "driver.h" |
|---|
| 39 |
|
|---|
| 40 |
sw_sounddata * |
|---|
| 41 |
sounddata_new_empty(gint nr_channels, gint sample_rate, gint sample_length) |
|---|
| 42 |
{ |
|---|
| 43 |
sw_sounddata *s; |
|---|
| 44 |
sw_framecount_t len; |
|---|
| 45 |
|
|---|
| 46 |
s = g_malloc (sizeof(sw_sounddata)); |
|---|
| 47 |
if (!s) |
|---|
| 48 |
return NULL; |
|---|
| 49 |
|
|---|
| 50 |
s->refcount = 1; |
|---|
| 51 |
|
|---|
| 52 |
s->format = format_new (nr_channels, sample_rate); |
|---|
| 53 |
|
|---|
| 54 |
s->nr_frames = (sw_framecount_t) sample_length; |
|---|
| 55 |
|
|---|
| 56 |
if (sample_length > 0) { |
|---|
| 57 |
len = frames_to_bytes (s->format, sample_length); |
|---|
| 58 |
|
|---|
| 59 |
s->data = g_malloc0 ((size_t)len); |
|---|
| 60 |
|
|---|
| 61 |
if (!(s->data)) { |
|---|
| 62 |
fprintf(stderr, "Unable to allocate %d bytes for sample data.\n", len); |
|---|
| 63 |
g_free(s); |
|---|
| 64 |
return NULL; |
|---|
| 65 |
#ifdef DEBUG |
|---|
| 66 |
} else { |
|---|
| 67 |
g_print ("g_malloc0'd %d bytes for new sounddata\n", len); |
|---|
| 68 |
#endif |
|---|
| 69 |
} |
|---|
| 70 |
} else { |
|---|
| 71 |
s->data = NULL; |
|---|
| 72 |
} |
|---|
| 73 |
|
|---|
| 74 |
s->sels = NULL; |
|---|
| 75 |
s->sels_mutex = g_mutex_new(); |
|---|
| 76 |
s->data_mutex = g_mutex_new(); |
|---|
| 77 |
|
|---|
| 78 |
return s; |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
void |
|---|
| 82 |
sounddata_clear_selection (sw_sounddata * sounddata) |
|---|
| 83 |
{ |
|---|
| 84 |
GList * gl; |
|---|
| 85 |
sw_sel * sel; |
|---|
| 86 |
|
|---|
| 87 |
for (gl = sounddata->sels; gl; gl = gl->next){ |
|---|
| 88 |
sel = (sw_sel*)gl->data; |
|---|
| 89 |
sel_free(sel); |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
g_list_free(sounddata->sels); |
|---|
| 93 |
|
|---|
| 94 |
sounddata->sels = NULL; |
|---|
| 95 |
}; |
|---|
| 96 |
|
|---|
| 97 |
void |
|---|
| 98 |
sounddata_destroy (sw_sounddata * sounddata) |
|---|
| 99 |
{ |
|---|
| 100 |
sounddata->refcount--; |
|---|
| 101 |
|
|---|
| 102 |
if (sounddata->refcount <= 0) { |
|---|
| 103 |
g_free (sounddata->data); |
|---|
| 104 |
g_mutex_free(sounddata->data_mutex); |
|---|
| 105 |
sounddata_clear_selection (sounddata); |
|---|
| 106 |
memset (sounddata, 0, sizeof (*sounddata)); |
|---|
| 107 |
g_free (sounddata); |
|---|
| 108 |
} |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
void |
|---|
| 112 |
sounddata_lock_selection (sw_sounddata * sounddata) |
|---|
| 113 |
{ |
|---|
| 114 |
g_mutex_lock (sounddata->sels_mutex); |
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
void |
|---|
| 118 |
sounddata_unlock_selection (sw_sounddata * sounddata) |
|---|
| 119 |
{ |
|---|
| 120 |
g_mutex_unlock (sounddata->sels_mutex); |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
guint |
|---|
| 124 |
sounddata_selection_nr_regions (sw_sounddata * sounddata) |
|---|
| 125 |
{ |
|---|
| 126 |
return g_list_length (sounddata->sels); |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
static gint |
|---|
| 130 |
sounddata_sel_needs_normalising (sw_sounddata *sounddata) |
|---|
| 131 |
{ |
|---|
| 132 |
GList * gl; |
|---|
| 133 |
sw_sel * osel = NULL, * sel; |
|---|
| 134 |
sw_framecount_t nr_frames; |
|---|
| 135 |
|
|---|
| 136 |
if(!sounddata->sels) return FALSE; |
|---|
| 137 |
|
|---|
| 138 |
nr_frames = sounddata->nr_frames; |
|---|
| 139 |
|
|---|
| 140 |
/* Seed osel with 'fake' iteration of following loop */ |
|---|
| 141 |
gl = sounddata->sels; |
|---|
| 142 |
osel = (sw_sel *)gl->data; |
|---|
| 143 |
if (osel->sel_start < 0 || osel->sel_end > nr_frames) |
|---|
| 144 |
return TRUE; |
|---|
| 145 |
|
|---|
| 146 |
gl = gl->next; |
|---|
| 147 |
for(; gl; gl = gl->next) { |
|---|
| 148 |
sel = (sw_sel *)gl->data; |
|---|
| 149 |
|
|---|
| 150 |
if (sel->sel_start < 0 || sel->sel_end > nr_frames) |
|---|
| 151 |
return TRUE; |
|---|
| 152 |
|
|---|
| 153 |
if(osel->sel_end >= sel->sel_start) { |
|---|
| 154 |
return TRUE; |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
if(osel->sel_end < sel->sel_end) { |
|---|
| 158 |
osel = sel; |
|---|
| 159 |
} |
|---|
| 160 |
} |
|---|
| 161 |
|
|---|
| 162 |
return FALSE; |
|---|
| 163 |
} |
|---|
| 164 |
|
|---|
| 165 |
/* |
|---|
| 166 |
* sounddata_normalise_selection(sounddata) |
|---|
| 167 |
* |
|---|
| 168 |
* normalise the selection of sounddata, ie. make sure there's |
|---|
| 169 |
* no overlaps and merge adjoining sections. |
|---|
| 170 |
*/ |
|---|
| 171 |
|
|---|
| 172 |
void |
|---|
| 173 |
sounddata_normalise_selection (sw_sounddata * sounddata) |
|---|
| 174 |
{ |
|---|
| 175 |
GList * gl; |
|---|
| 176 |
GList * nsels = NULL; |
|---|
| 177 |
sw_sel * osel = NULL, * sel; |
|---|
| 178 |
sw_framecount_t nr_frames; |
|---|
| 179 |
|
|---|
| 180 |
if (!sounddata_sel_needs_normalising(sounddata)) return; |
|---|
| 181 |
|
|---|
| 182 |
nr_frames = sounddata->nr_frames; |
|---|
| 183 |
|
|---|
| 184 |
/* Seed osel with 'fake' iteration of following loop */ |
|---|
| 185 |
gl = sounddata->sels; |
|---|
| 186 |
sel = (sw_sel *)gl->data; |
|---|
| 187 |
sel->sel_start = CLAMP(sel->sel_start, 0, nr_frames); |
|---|
| 188 |
sel->sel_end = CLAMP(sel->sel_end, 0, nr_frames); |
|---|
| 189 |
|
|---|
| 190 |
osel = sel_copy(sel); |
|---|
| 191 |
|
|---|
| 192 |
gl = gl->next; |
|---|
| 193 |
|
|---|
| 194 |
for (; gl; gl = gl->next) { |
|---|
| 195 |
sel = (sw_sel *)gl->data; |
|---|
| 196 |
sel->sel_start = CLAMP(sel->sel_start, 0, nr_frames); |
|---|
| 197 |
sel->sel_end = CLAMP(sel->sel_end, 0, nr_frames); |
|---|
| 198 |
|
|---|
| 199 |
/* Check for an overlap */ |
|---|
| 200 |
if(osel->sel_end >= sel->sel_start) { |
|---|
| 201 |
|
|---|
| 202 |
/* If sel is completely contained in osel, ignore it. */ |
|---|
| 203 |
if(osel->sel_end > sel->sel_end) { |
|---|
| 204 |
continue; |
|---|
| 205 |
} |
|---|
| 206 |
|
|---|
| 207 |
/* Set: osel = osel INTERSECT sel |
|---|
| 208 |
* we already know osel->sel_start <= sel->sel_start */ |
|---|
| 209 |
osel->sel_end = sel->sel_end; |
|---|
| 210 |
|
|---|
| 211 |
} else { |
|---|
| 212 |
/* No more overlaps with osel; insert it in nsels, and |
|---|
| 213 |
* reset osel. */ |
|---|
| 214 |
if (osel->sel_start == osel->sel_end) { |
|---|
| 215 |
g_free (osel); |
|---|
| 216 |
} else { |
|---|
| 217 |
nsels = g_list_insert_sorted(nsels, osel, (GCompareFunc)sel_cmp); |
|---|
| 218 |
} |
|---|
| 219 |
osel = sel_copy(sel); |
|---|
| 220 |
} |
|---|
| 221 |
} |
|---|
| 222 |
|
|---|
| 223 |
/* Insert the last created osel */ |
|---|
| 224 |
if (osel->sel_start == osel->sel_end) { |
|---|
| 225 |
g_free (osel); |
|---|
| 226 |
} else { |
|---|
| 227 |
nsels = g_list_insert_sorted(nsels, osel, (GCompareFunc)sel_cmp); |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
/* Clear the old selection */ |
|---|
| 231 |
sounddata_clear_selection (sounddata); |
|---|
| 232 |
|
|---|
| 233 |
/* Set the newly created (normalised) selection */ |
|---|
| 234 |
sounddata->sels = nsels; |
|---|
| 235 |
|
|---|
| 236 |
} |
|---|
| 237 |
|
|---|
| 238 |
void |
|---|
| 239 |
sounddata_add_selection (sw_sounddata * sounddata, sw_sel * sel) |
|---|
| 240 |
{ |
|---|
| 241 |
sounddata->sels = |
|---|
| 242 |
g_list_insert_sorted(sounddata->sels, sel, (GCompareFunc)sel_cmp); |
|---|
| 243 |
} |
|---|
| 244 |
|
|---|
| 245 |
sw_sel * |
|---|
| 246 |
sounddata_add_selection_1 (sw_sounddata * sounddata, sw_framecount_t start, sw_framecount_t end) |
|---|
| 247 |
{ |
|---|
| 248 |
sw_sel * sel; |
|---|
| 249 |
|
|---|
| 250 |
sel = sel_new (start, end); |
|---|
| 251 |
|
|---|
| 252 |
sounddata_add_selection(sounddata, sel); |
|---|
| 253 |
|
|---|
| 254 |
return sel; |
|---|
| 255 |
} |
|---|
| 256 |
|
|---|
| 257 |
sw_sel * |
|---|
| 258 |
sounddata_set_selection_1 (sw_sounddata * sounddata, sw_framecount_t start, sw_framecount_t end) |
|---|
| 259 |
{ |
|---|
| 260 |
sounddata_clear_selection (sounddata); |
|---|
| 261 |
|
|---|
| 262 |
return sounddata_add_selection_1 (sounddata, start, end); |
|---|
| 263 |
} |
|---|
| 264 |
|
|---|
| 265 |
gint |
|---|
| 266 |
sounddata_selection_nr_frames (sw_sounddata * sounddata) |
|---|
| 267 |
{ |
|---|
| 268 |
gint nr_frames = 0; |
|---|
| 269 |
GList * gl; |
|---|
| 270 |
sw_sel * sel; |
|---|
| 271 |
|
|---|
| 272 |
for (gl = sounddata->sels; gl; gl = gl->next) { |
|---|
| 273 |
sel = (sw_sel *)gl->data; |
|---|
| 274 |
|
|---|
| 275 |
nr_frames += sel->sel_end - sel->sel_start; |
|---|
| 276 |
} |
|---|
| 277 |
|
|---|
| 278 |
return nr_frames; |
|---|
| 279 |
} |
|---|
| 280 |
|
|---|
| 281 |
sw_framecount_t |
|---|
| 282 |
sounddata_selection_width (sw_sounddata * sounddata) |
|---|
| 283 |
{ |
|---|
| 284 |
GList * gl; |
|---|
| 285 |
sw_sel * sel; |
|---|
| 286 |
sw_framecount_t start, end; |
|---|
| 287 |
|
|---|
| 288 |
if ((gl = sounddata->sels) == NULL) return 0; |
|---|
| 289 |
sel = (sw_sel *)gl->data; |
|---|
| 290 |
start = sel->sel_start; |
|---|
| 291 |
|
|---|
| 292 |
gl = g_list_last (sounddata->sels); |
|---|
| 293 |
sel = (sw_sel *)gl->data; |
|---|
| 294 |
end = sel->sel_end; |
|---|
| 295 |
|
|---|
| 296 |
return (end - start); |
|---|
| 297 |
} |
|---|
| 298 |
|
|---|
| 299 |
void |
|---|
| 300 |
sounddata_selection_translate (sw_sounddata * sounddata, gint delta) |
|---|
| 301 |
{ |
|---|
| 302 |
GList * gl; |
|---|
| 303 |
sw_sel * sel; |
|---|
| 304 |
|
|---|
| 305 |
for (gl = sounddata->sels; gl; gl = gl->next) { |
|---|
| 306 |
sel = (sw_sel *)gl->data; |
|---|
| 307 |
|
|---|
| 308 |
sel->sel_start += delta; |
|---|
| 309 |
sel->sel_end += delta; |
|---|
| 310 |
} |
|---|
| 311 |
|
|---|
| 312 |
/* XXX: Crop any regions outside of [0, nr_frames] */ |
|---|
| 313 |
sounddata_normalise_selection (sounddata); |
|---|
| 314 |
} |
|---|
| 315 |
|
|---|
| 316 |
void |
|---|
| 317 |
sounddata_selection_scale (sw_sounddata * sounddata, gfloat scale) |
|---|
| 318 |
{ |
|---|
| 319 |
GList * gl; |
|---|
| 320 |
sw_sel * sel; |
|---|
| 321 |
sw_framecount_t sels_start; |
|---|
| 322 |
|
|---|
| 323 |
if ((gl = sounddata->sels) == NULL) return; |
|---|
| 324 |
|
|---|
| 325 |
sel = (sw_sel *)gl->data; |
|---|
| 326 |
sels_start = sel->sel_start; |
|---|
| 327 |
|
|---|
| 328 |
for (gl = sounddata->sels; gl; gl = gl->next) { |
|---|
| 329 |
sel = (sw_sel *)gl->data; |
|---|
| 330 |
|
|---|
| 331 |
sel->sel_start = sels_start + ((sel->sel_start - sels_start) * scale); |
|---|
| 332 |
sel->sel_end = sels_start + ((sel->sel_end - sels_start) * scale); |
|---|
| 333 |
} |
|---|
| 334 |
|
|---|
| 335 |
/* XXX: Crop any regions outside of [0, nr_frames] */ |
|---|
| 336 |
sounddata_normalise_selection (sounddata); |
|---|
| 337 |
} |
|---|
| 338 |
|
|---|
| 339 |
/* |
|---|
| 340 |
* sounddata_copyin_selection (sounddata1, sounddata2) |
|---|
| 341 |
* |
|---|
| 342 |
* copies the selection of sounddata1 into sounddata2. If sounddata2 previously |
|---|
| 343 |
* had a selection, the two are merged. |
|---|
| 344 |
*/ |
|---|
| 345 |
void |
|---|
| 346 |
sounddata_copyin_selection (sw_sounddata * sounddata1, sw_sounddata * sounddata2) |
|---|
| 347 |
{ |
|---|
| 348 |
GList * gl; |
|---|
| 349 |
sw_sel * sel, *sel2; |
|---|
| 350 |
|
|---|
| 351 |
for (gl = sounddata1->sels; gl; gl = gl->next) { |
|---|
| 352 |
sel = (sw_sel *)gl->data; |
|---|
| 353 |
|
|---|
| 354 |
sel2 = sel_copy (sel); |
|---|
| 355 |
sounddata_add_selection (sounddata2, sel2); |
|---|
| 356 |
} |
|---|
| 357 |
|
|---|
| 358 |
sounddata_normalise_selection (sounddata2); |
|---|
| 359 |
} |
|---|