delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/02/17/13:21:59

From: M DOT A DOT Bukin AT inp DOT nsk DOT su
To: djgpp AT delorie DOT com
Subject: Re: Fast reading of multiple keypresses in Allegro
References: <8D53104ECD0CD211AF4000A0C9D60AE353FA26 AT probe-2 DOT acclaim-euro DOT net> <7aenjo$mmm$1 AT news5 DOT svr DOT pol DOT co DOT uk>
Date: 17 Feb 1999 23:54:09 +0600
In-Reply-To: "Andrew Davidson"'s message of "Wed, 17 Feb 1999 13:51:38 -0000"
Message-ID: <201zjovrum.fsf@Sky.inp.nsk.su>
Lines: 56
X-Mailer: Gnus v5.5/Emacs 19.34
Reply-To: djgpp AT delorie DOT com

"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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019