1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
|
/*****************************************************************************
* Gnome Wave Cleaner Version 0.19
* Copyright (C) 2003 Jeffrey J. Welty
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*******************************************************************************/
/* soundfile.c some functions to manipulate the sound file ...frank 4.10.03 */
#include <stdlib.h>
#include <stdio.h>
#include <sndfile.h>
#include "soundfile.h"
#include "gwc.h"
extern SNDFILE *sndfile;
extern SF_INFO sfinfo;
static void perr(char *text)
{
int err = sf_error(sndfile);
puts("##########################################################");
puts(text);
puts(sf_error_number(err));
}
long soundfile_count_samples_in_file(char *filename)
{
sf_count_t sample_count = -1;
SNDFILE *sndfile_new;
SF_INFO sfinfo_new = sfinfo;
sfinfo_new.format = 0;
sndfile_new = sf_open(filename, SFM_READ, &sfinfo_new);
if (sndfile_new != NULL) {
sample_count = sf_seek(sndfile_new, 0, SEEK_END);
sf_close(sndfile_new);
}
/* printf("soundfile_count_samples_in_file: %lld\n", sample_count); */
return sample_count;
}
int soundfile_save_file(char *filename, long lpos, long lsample_count,
int status_info)
{
sf_count_t pos = lpos;
sf_count_t sample_count = lsample_count;
int rc = -1;
if (sf_seek(sndfile, pos, SEEK_SET|SFM_READ) == pos) {
int channels = sfinfo.channels;
sf_count_t buffer_size = SBW*1000;
int *buffer = calloc(channels*buffer_size, sizeof(int));
if (buffer != NULL) {
SNDFILE *sndfile_new;
SF_INFO sfinfo_new = sfinfo;
sndfile_new = sf_open(filename, SFM_WRITE, &sfinfo_new);
if (sndfile_new != NULL) {
sf_count_t processed = 0;
sf_count_t read_size;
while (processed < sample_count) {
if (buffer_size > sample_count - processed)
buffer_size = sample_count - processed;
read_size = sf_readf_int(sndfile, buffer, buffer_size);
if (read_size > 0)
sf_writef_int(sndfile_new, buffer, read_size);
processed += buffer_size;
if (status_info)
update_progress_bar((gfloat)processed/sample_count,
PROGRESS_UPDATE_INTERVAL, FALSE);
}
sf_close(sndfile_new);
rc = 0;
}
free(buffer);
}
}
return rc;
}
int soundfile_load_file(char *filename, long lpos, long lsample_count,
int status_info)
{
sf_count_t pos = lpos;
sf_count_t sample_count = lsample_count;
int rc = -1;
if (sf_seek(sndfile, pos, SEEK_SET|SFM_WRITE) == pos) {
int channels = sfinfo.channels;
sf_count_t buffer_size = SBW*1000;
int *buffer = calloc(channels*buffer_size, sizeof(int));
if (buffer != NULL) {
SNDFILE *sndfile_new;
SF_INFO sfinfo_new = sfinfo;
sfinfo_new.format = 0;
sndfile_new = sf_open(filename, SFM_READ, &sfinfo_new);
if (sndfile_new != NULL) {
sf_count_t processed = 0;
sf_count_t read_size;
if (sample_count > sfinfo_new.frames)
sample_count = sfinfo_new.frames;
while (processed < sample_count) {
if (buffer_size > sample_count - processed)
buffer_size = sample_count - processed;
read_size = sf_readf_int(sndfile_new, buffer, buffer_size);
if (read_size > 0)
sf_writef_int(sndfile, buffer, read_size);
processed += buffer_size;
if (status_info)
update_progress_bar((gfloat)processed/sample_count,
PROGRESS_UPDATE_INTERVAL, FALSE);
}
sf_close(sndfile_new);
rc = 0;
/* remove(filename); */
}
free(buffer);
}
}
return rc;
}
static void adjust_all_marker_positions(long start, long delta)
{
adjust_song_marker_positions(start, delta);
adjust_marker_positions(start, delta);
}
long soundfile_count_samples(void)
{
sf_count_t curr = sf_seek(sndfile, 0, SEEK_SET|SFM_READ);
sf_count_t rc = sf_seek(sndfile, 0, SEEK_END|SFM_READ);
sf_seek(sndfile, curr, SEEK_SET|SFM_READ);
return rc;
}
/* simply write 'sample_count' silence-samples at 'pos'
* (overwrite existing samples)
*/
static int write_silence(sf_count_t pos, sf_count_t sample_count)
{
int rc = 0;
int channels = sfinfo.channels;
sf_count_t buffer_size = SBW*100;
int *buffer = calloc(channels*buffer_size, sizeof(int));
/* printf("write_silence: pos=%lld, sample_count=%lld\n", */
/* pos, sample_count); */
if (buffer == NULL) {
warning("Out of Memory");
return -1;
}
/* go to position ... */
if (sf_seek(sndfile, pos, SEEK_SET|SFM_WRITE) < 0) {
perr("write_silence: sf_seek write pointer");
warning("Libsndfile reports write pointer seek error in audio file");
rc = -1;
}
else {
/* ... and write sample_count silence */
while (sample_count > 0) {
if (sample_count - buffer_size < 0)
buffer_size = sample_count;
if (sf_writef_int(sndfile, buffer, buffer_size) != buffer_size) {
perr("write_silence: sf_writef_int");
warning("Libsndfile reports write error in audio file");
rc = -1;
break;
}
sample_count -= buffer_size;
}
}
free(buffer);
/* printf("write_silence: rc=%d\n", rc); */
return rc;
}
/* append 'sample_count' samples and shift all samples from 'first_pos'
* to end of file (this creates 'sample_count' samples at 'first_pos')
*/
int soundfile_shift_samples_right(long lfirst_pos, long lsample_count,
int status_info)
{
sf_count_t first_pos = lfirst_pos;
sf_count_t sample_count = lsample_count;
sf_count_t end_pos = sf_seek(sndfile, 0, SEEK_END|SFM_READ);
if (sample_count <= 0) {
puts("soundfile_shift_samples_right: no samples to move");
return 0;
}
/* printf("soundfile_shift_samples_right: " */
/* "first_pos=%lld, shift=%lld, samples in file=%lld\n", */
/* first_pos, sample_count, end_pos); */
if (first_pos < end_pos) {
sf_count_t processed = 0;
sf_count_t read_pos, write_pos;
int channels = sfinfo.channels;
#define TMPBUFSIZE (4*SBW*1000)
int buffer[TMPBUFSIZE];
sf_count_t buffer_size = TMPBUFSIZE/channels;
/* go to end position and append enough space for the new data */
if (write_silence(end_pos, sample_count) < 0) {
return -1;
}
/* loop reading-writing from end */
write_pos = end_pos + sample_count;
read_pos = end_pos;
while (read_pos > first_pos) {
if (buffer_size > read_pos - first_pos)
buffer_size = read_pos - first_pos;
write_pos -= buffer_size;
read_pos -= buffer_size;
/* printf("soundfile_shift_samples_right: " */
/* "read_pos=%lld, write_pos=%lld, diff=%lld, buffer_size=%lld\n", */
/* read_pos, write_pos, write_pos-read_pos, buffer_size); */
/* start position for reading */
if (sf_seek(sndfile, read_pos, SEEK_SET|SFM_READ) < 0) {
perr("soundfile_shift_samples_right: sf_seek read pointer");
warning("Libsndfile reports read pointer seek error in audio file");
return -1;
}
if (sf_readf_int(sndfile, buffer, buffer_size) != buffer_size) {
perr("soundfile_shift_samples_right: sf_readf_int");
warning("Libsndfile reports read error in audio file");
return -1;
}
/* start position for writing */
if (sf_seek(sndfile, write_pos, SEEK_SET|SFM_WRITE) < 0) {
perr("soundfile_shift_samples_right: sf_seek write pointer");
warning("Libsndfile reports write pointer seek error in audio file");
return -1;
}
if (sf_writef_int(sndfile, buffer, buffer_size) != buffer_size) {
perr("soundfile_shift_samples_right: sf_writef_int");
warning("Libsndfile reports write error in audio file");
return -1;
}
processed += buffer_size;
if (status_info)
update_progress_bar((gfloat)processed/(end_pos-first_pos),
PROGRESS_UPDATE_INTERVAL, FALSE);
/* printf("soundfile_shift_samples_right: processed=%lld (%f.2)\n", */
/* processed, (gfloat)processed/(end_pos-first_pos)); */
}
}
/* printf("soundfile_shift_samples_right: samples in file=%lld\n", */
/* sf_seek(sndfile, 0, SEEK_END|SFM_READ)); */
adjust_all_marker_positions(first_pos, sample_count);
return 0;
}
/* shift all samples from 'first_pos' to begin of file
* (this removes 'sample_count' samples at end of file)
*/
int soundfile_shift_samples_left(long lfirst_pos, long lsample_count,
int status_info)
{
/* if 'last_pos' is equal 'end_pos' simply truncate the end
* else move sample i from 'last_pos' to the 'first_pos' position
* (for all i between 'last_pos' and end of file)
* and then truncate 'last_pos - first_pos' samples at the end.
*/
sf_count_t first_pos = lfirst_pos;
sf_count_t sample_count = lsample_count;
sf_count_t end_pos = sf_seek(sndfile, 0, SEEK_END|SFM_READ);
sf_count_t last_pos = first_pos + sample_count;
if (sample_count <= 0) {
puts("soundfile_shift_samples_left: no samples to move");
return 0;
}
/* printf("soundfile_shift_samples_left: " */
/* "first_pos=%lld, shift=%lld, samples in file=%lld\n", */
/* first_pos, sample_count, end_pos); */
if (last_pos < end_pos)
{
sf_count_t processed = 0;
sf_count_t buffer_size;
int channels = sfinfo.channels;
#define TMPBUFSIZE (4*SBW*1000)
int buffer[TMPBUFSIZE];
/* start position for writing */
if (sf_seek(sndfile, first_pos, SEEK_SET|SFM_WRITE) < 0)
{
perr("soundfile_shift_samples_left: sf_seek write pointer");
warning("Libsndfile reports write pointer seek error in audio file");
return -1;
}
/* start position for reading */
if (sf_seek(sndfile, last_pos, SEEK_SET|SFM_READ) < 0)
{
perr("soundfile_shift_samples_left: sf_seek read pointer");
warning("Libsndfile reports read pointer seek error in audio file");
return -1;
}
/* loop to end of file */
buffer_size = TMPBUFSIZE/channels;
while ((buffer_size = sf_readf_int(sndfile, buffer, buffer_size)) > 0)
{
if (sf_writef_int(sndfile, buffer, buffer_size) != buffer_size) {
perr("soundfile_shift_samples_left: sf_writef_int");
warning("Libsndfile reports write error in audio file");
return -1;
}
processed += buffer_size;
if (status_info)
update_progress_bar((gfloat)processed/(end_pos-first_pos-sample_count),
PROGRESS_UPDATE_INTERVAL, FALSE);
/* printf("soundfile_shift_samples_left: processed=%lld (%f.2)\n", */
/* processed, (gfloat)processed/(end_pos-first_pos-sample_count)); */
}
}
/* truncate file size for '-sample_count' samples */
end_pos -= sample_count;
if (sf_command(sndfile, SFC_FILE_TRUNCATE, &end_pos, sizeof(end_pos)))
{
perr("soundfile_shift_samples_left: sf_command");
warning("Libsndfile reports truncation of audio file failed");
return -1;
}
/* printf("soundfile_shift_samples_left: samples in file=%lld\n", */
/* sf_seek(sndfile, 0, SEEK_END|SFM_READ)); */
adjust_all_marker_positions(first_pos, -sample_count);
return 0;
}
/* insert 'sample_count' samples at sample 'insert_pos'
* to the soundfile.
* 'insert_pos' == -1 means append to end of file.
*/
int soundfile_insert_samples(long linsert_pos, long lsample_count,
int *sample_data, int status_info)
{
sf_count_t insert_pos = linsert_pos;
sf_count_t sample_count = lsample_count;
sf_count_t end_pos = sf_seek(sndfile, 0, SEEK_END|SFM_READ);
if (insert_pos < 0 || insert_pos > end_pos)
insert_pos = end_pos;
if (soundfile_shift_samples_right(insert_pos, sample_count, status_info) < 0) {
return -1;
}
/* go to insert position 'insert_pos'... */
if (sf_seek(sndfile, insert_pos, SEEK_SET|SFM_WRITE) < 0) {
perr("soundfile_insert_samples: sf_seek write pointer");
warning("Libsndfile reports write pointer seek error in audio file");
return -1;
}
/* ...and insert new data */
if (sf_writef_int(sndfile, sample_data, sample_count) != sample_count) {
perr("soundfile_insert_samples: append with sf_writef_int");
warning("Libsndfile reports write error in audio file");
return -1;
}
return 0;
}
int soundfile_insert_silence(long linsert_pos, long lsample_count,
int status_info)
{
sf_count_t insert_pos = linsert_pos;
sf_count_t sample_count = lsample_count;
sf_count_t end_pos = sf_seek(sndfile, 0, SEEK_END|SFM_READ);
int rc = 0;
if (insert_pos < 0 || insert_pos > end_pos)
insert_pos = end_pos;
if (soundfile_shift_samples_right(insert_pos, sample_count, status_info) < 0) {
rc = -1;
}
else if (write_silence(insert_pos, sample_count) < 0) {
rc = -1;
}
return rc;
}
/* remove 'sample_count' samples from 'first_pos' sample.
* 'sample_count' == -1 means to end of file.
*/
int soundfile_remove_samples(long lfirst_pos, long lsample_count,
int status_info)
{
sf_count_t first_pos = lfirst_pos;
sf_count_t sample_count = lsample_count;
sf_count_t end_pos = sf_seek(sndfile, 0, SEEK_END|SFM_READ);
if (first_pos < 0 || first_pos > end_pos)
first_pos = end_pos;
if (sample_count < 0 || first_pos + sample_count - 1 > end_pos)
sample_count = end_pos - first_pos + 1;
return soundfile_shift_samples_left(first_pos, sample_count, status_info);
}
|