Digital Sound & Music: Concepts, Applications, & Science, Chapter 3, last updated 6/25/2013
49
switch (scaleType) {
case MAJOR :
break1 = 3;
break2 = 7;
break;
case MINOR :
break1 = 2;
break2 = 5;
break;
case BACK :
return;
default :
playScale(deviceID);
}
arraySize = LENGTH * RATE * CHANNELS;
for (note = off = 0; note 8; ++note, off += t) {
if (note == 0)
steps = 0;
else if (note == break1 || note == break2)
steps = 1;
else steps = 2;
f *= pow(SEMITONE, steps);
for (t = 0; t arraySize; ++t)
buf[t + off] = floor(SAMPLE_MAX*sin(2*PI*f*t/RATE));
}
arraySize = 8 * LENGTH * RATE * SIZE * CHANNELS;
writeToSoundDevice(buf, arraySize, deviceID);
delete buf;
return;
}
int main(){
int deviceID, arg, status, index;
deviceID = open("/dev/dsp", O_WRONLY, 0);
if (deviceID 0)
perror("Opening /dev/dsp failed\n");
arg = SIZE * 8;
status = ioctl(deviceID, SOUND_PCM_WRITE_BITS, &arg);
if (status == -1)
perror("Unable to set sample size\n");
arg = CHANNELS;
status = ioctl(deviceID, SOUND_PCM_WRITE_CHANNELS, &arg);
if (status == -1)
perror("Unable to set number of channels\n");
arg = RATE;
status = ioctl(deviceID, SOUND_PCM_WRITE_RATE, &arg);
if (status == -1)
perror("Unable to set sampling rate\n");
while (true)
playScale(deviceID);
}
Algorithm 3.2
3.3.6 Experimenting with Music in Java
Previous Page Next Page