Sound Designer
Editing DSP Functions

Algorithms

```int findPeak(short *array, int size)
{
int i, peakIndex = 0;
short absValue = 0;

for (i = 0; i < size; i++)
{
if (array[i] < 0)
absValue = -array[i];
else
absValue = array[i];
if (absValue > peakValue)
{
peakValue = absValue;
peakIndex = i;
}
}
return(peakIndex);
}

void normalize(short *array, int size)
{
int i, peakIndex;;
float mult;

peakIndex = findPeak(array, size);
if (array[peakIndex] == 0)
return;                    /* avoid divide by zero */

mult = 32767 / (float)array[peakIndex];
if (mult < 0)
mult = -mult;
for (i = 0; i < size; i++)
array[i] = mult * array[i];
}

void reverse(short *array, int size)
{
int i;
short temp;

for (i = 0; i < size / 2; i++)
{
temp = array[i];
array[i] = array[size - i];
array[size - i] = temp;
}
}

void silence(short *array, int size)
{
int i;

for (i = 0; i < size; i++)
array[i] = 0;
}

void trim(short *array, int size, int start, int end)
{
int i;
int len = end - start + 1;

for (i = 0; i < len; i++)  /*  Or silence(array, start); */
array[i] = array[start + i];
for (i = len; i < size; i++)
array[i] = 0;              /* silence(&array[end + 1}, size - end); */
}

void invert(short *array, int size)
{
int i;

for (i = 0; i < size; i++)
array[i] = -array[i];
}

{
int i;
float mult;

if (size < 2)
return;

mult = 1.0 / (size - 1);
array[0] = 0;
for (i = 1; i < size - 1; i++)
array[i] = i * mult * array[i] + 0.5;
}

{
int i;
float mult;

if (size < 2)
return;

mult = 1.0 / (size - 1);
array[size - 1] = 0;
for (i = 1; i < size - 1; i++)
array[size - i] = i * mult * array[size - i] + 0.5;
}

short *cut(short *array, int size, int start, int len)
{
int i;
short *cutArray = malloc(sizeof(short) * len);  /* create new array */

for (i = 0; i < len; i++)    /* copy */
cutArray[i] = array[start + i];
for (i = start; i < size - len; i++)     /* shift to the left */
array[i] = array[i + len];

return(cutArray);  /* return new size */
}

short *copy(short *array, int size, int start, int len)
{
int i;
short *copyArray = malloc(sizeof(short) * len);  /* create new array */

for (i = 0; i < len; i++)    /* copy */
copyArray[i] = array[start + i];

return(copyArray);  /* return new size */
}

void paste(short *array, int size, int start, short *new, int sizeNew)
{
int i;    /* assumes there's enough space in array */

for (i = size - 1; i >= start; i--)  /* shift to the right */
array[i + sizeNew] = array[i];

for (i = 0; i < sizeNew; i++)
array[start + i] = new[i];
}

void replace(short *array, int size, int start, short *new, int sizeNew)
{
int i;    /* assumes there's enough space in array */

for (i = 0; i < sizeNew; i++)
array[start + i] = new[i];
}
```

Ichiro Fujinaga 1998.09.09