Mail Archives: djgpp/1999/02/17/13:21:59
"Andrew Davidson" <andrew AT lemure DOT freeserve DOT co DOT uk> writes:
> I'm trying to create noise by playing a very short sample as fast as
> possible. The sample is created using:
>
> SAMPLE *samp;
> samp=create_sample(8, 0, 1000, 1);
`len' is a number of samples. 1000 samples playing at 1000 Hz would
play for 1 second. 1 sample at 1 kHz would play for 1 msec (perhaps
it will produce just a click).
data field of sample is initialized to zeros by create_sample, you
will need to fill it with data. Example
#include <stdlib.h>
#include <allegro.h>
int
main (void)
{
int i;
SAMPLE *s;
allegro_init ();
install_keyboard ();
install_sound (DIGI_AUTODETECT, MIDI_AUTODETECT, 0);
s = create_sample (8, 0, 1000, 1000);
if (s == 0)
return 1;
for (i = 0; i < 1000; i++)
((unsigned char*) (s->data))[i] = random ();
play_sample (s, 255, 128, 1000, 1);
readkey ();
return 0;
}
You can make noise to sound differently by replacing random () with
something else, for example
#include <math.h>
#include <values.h>
...
(int) (127. * sin (3.1415 * i / 1000.)
* (double) random () / (double) MAXINT)
But I'm not sure that this one will be correct for all sound cards,
because of differences between signed/unsigned samples.
--
Michael Bukin
- Raw text -