| 1 |
/*************************************************************************/ |
| 2 |
/* */ |
| 3 |
/* Language Technologies Institute */ |
| 4 |
/* Carnegie Mellon University */ |
| 5 |
/* Copyright (c) 2000 */ |
| 6 |
/* All Rights Reserved. */ |
| 7 |
/* */ |
| 8 |
/* Permission is hereby granted, free of charge, to use and distribute */ |
| 9 |
/* this software and its documentation without restriction, including */ |
| 10 |
/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 11 |
/* distribute, sublicense, and/or sell copies of this work, and to */ |
| 12 |
/* permit persons to whom this work is furnished to do so, subject to */ |
| 13 |
/* the following conditions: */ |
| 14 |
/* 1. The code must retain the above copyright notice, this list of */ |
| 15 |
/* conditions and the following disclaimer. */ |
| 16 |
/* 2. Any modifications must be clearly marked as such. */ |
| 17 |
/* 3. Original authors' names are not deleted. */ |
| 18 |
/* 4. The authors' names are not used to endorse or promote products */ |
| 19 |
/* derived from this software without specific prior written */ |
| 20 |
/* permission. */ |
| 21 |
/* */ |
| 22 |
/* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */ |
| 23 |
/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ |
| 24 |
/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ |
| 25 |
/* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */ |
| 26 |
/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ |
| 27 |
/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ |
| 28 |
/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ |
| 29 |
/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ |
| 30 |
/* THIS SOFTWARE. */ |
| 31 |
/* */ |
| 32 |
/*************************************************************************/ |
| 33 |
/* Author: Alan W Black ([email protected]) */ |
| 34 |
/* Date: August 2000 */ |
| 35 |
/*************************************************************************/ |
| 36 |
/* */ |
| 37 |
/* Waveforms */ |
| 38 |
/* */ |
| 39 |
/*************************************************************************/ |
| 40 |
#include "cst_string.h" |
| 41 |
#include "cst_val.h" |
| 42 |
#include "cst_wave.h" |
| 43 |
|
| 44 |
CST_VAL_REGISTER_TYPE(wave,cst_wave) |
| 45 |
|
| 46 |
cst_wave *new_wave() |
| 47 |
{ |
| 48 |
cst_wave *w = cst_alloc(struct cst_wave_struct,1); |
| 49 |
w->type = NULL; |
| 50 |
w->num_samples = 0; |
| 51 |
w->samples = NULL; |
| 52 |
return w; |
| 53 |
} |
| 54 |
|
| 55 |
void delete_wave(cst_wave *w) |
| 56 |
{ |
| 57 |
if (w) |
| 58 |
{ |
| 59 |
cst_free(w->samples); |
| 60 |
cst_free(w); |
| 61 |
} |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
void cst_wave_resize(cst_wave *w,int samples, int num_channels) |
| 66 |
{ |
| 67 |
short *ns; |
| 68 |
|
| 69 |
if (!w) |
| 70 |
{ |
| 71 |
cst_errmsg("cst_wave_resize: null wave given to resize\n"); |
| 72 |
cst_error(); |
| 73 |
} |
| 74 |
ns = cst_alloc(short,samples*num_channels); |
| 75 |
if (num_channels == w->num_channels) |
| 76 |
memmove(ns,w->samples, |
| 77 |
sizeof(short) * |
| 78 |
num_channels * |
| 79 |
(samples < w->num_samples ? samples : w->num_samples)); |
| 80 |
cst_free(w->samples); |
| 81 |
w->samples = ns; |
| 82 |
w->num_samples = samples; |
| 83 |
w->num_channels = num_channels; |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
void cst_wave_rescale(cst_wave *w, int factor) |
| 88 |
{ |
| 89 |
int i; |
| 90 |
|
| 91 |
for (i = 0; i < w->num_samples; ++i) |
| 92 |
w->samples[i] = ((int)w->samples[i] * factor) / 65536; |
| 93 |
} |
| 94 |
|
| 95 |
cst_wave *copy_wave(const cst_wave *w) |
| 96 |
{ |
| 97 |
cst_wave *n = new_wave(); |
| 98 |
|
| 99 |
cst_wave_resize(n,w->num_samples,w->num_channels); |
| 100 |
n->sample_rate = w->sample_rate; |
| 101 |
n->num_channels = w->num_channels; |
| 102 |
n->type = w->type; |
| 103 |
memcpy(n->samples,w->samples,sizeof(short)*w->num_samples*w->num_channels); |
| 104 |
return n; |
| 105 |
} |
| 106 |
|
| 107 |
cst_wave *concat_wave(cst_wave *dest, const cst_wave *src) |
| 108 |
{ |
| 109 |
int orig_nsamps; |
| 110 |
|
| 111 |
if (dest->num_channels != src->num_channels) |
| 112 |
{ |
| 113 |
cst_errmsg("concat_wave: channel count mismatch (%d != %d)\n", |
| 114 |
dest->num_channels, src->num_channels); |
| 115 |
cst_error(); |
| 116 |
} |
| 117 |
if (dest->sample_rate != src->sample_rate) |
| 118 |
{ |
| 119 |
cst_errmsg("concat_wave: sample rate mismatch (%d != %d)\n", |
| 120 |
dest->sample_rate, src->sample_rate); |
| 121 |
cst_error(); |
| 122 |
} |
| 123 |
|
| 124 |
orig_nsamps = dest->num_samples * dest->num_channels; |
| 125 |
cst_wave_resize(dest, dest->num_samples + src->num_samples, |
| 126 |
dest->num_channels); |
| 127 |
memcpy(dest->samples + orig_nsamps, src->samples, |
| 128 |
src->num_samples * src->num_channels * sizeof(short)); |
| 129 |
|
| 130 |
return dest; |
| 131 |
} |