White noise generator

If you set a frequency it's not white noise anymore.
I guess, balanga meant "sampling frequency".
Can I do something to specify frequency, volume and duration?
Volume = amplitude, you can cat /dev/random into a regular file, then process is by normalizing to a level. However, it will be tricky in pure shell. In case of writing a program in a language you could simply generate a series of random values within specific range: regular sound cards operate with 16-bit values, so the max volume is the range of 0...65535.
Regarding the sampling frequency: you can set your sound card frequency, but not sure whether it is possibly with pure shell.
 
Here comes a piece of C code, which I posted recently on the forums. I only extended this with a duration parameter (compare with: https://forums.freebsd.org/threads/61144/#post-352026)
Code:
// compile: clang dsp-sinus.c -lm -o dsp-sinus
//
// usage:   dsp-sinus [freqency] [level] [duration]   (defaults: freqency 1000 Hz, level 0.5, duration 30 s)
// example: dsp-sinus 5000 0.75 45

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>


#define pi2 6.2831853071795865

int main(int argc, const char *argv[])
{
   int dsp = open("/dev/dsp0", O_RDWR);
   if (dsp != -1)
   {
      unsigned param;

      param = AFMT_S32_LE;
      if (ioctl(dsp, SNDCTL_DSP_SETFMT, &param) == -1 || param != AFMT_S32_LE)
      {
         close(dsp);
         printf("Error setting the input format (0x%X)\n", param);
         return -1;
      }

      param = 2;
      if (ioctl(dsp, SNDCTL_DSP_CHANNELS, &param) == -1 || param != 2)
      {
         close(dsp);
         printf("Error setting stereo output %d\n", param);
         return -2;
      }

      // force a sampling rate of 48 kHz
      param = 48000;
      if (ioctl(dsp, SNDCTL_DSP_SPEED, &param) != -1)
         printf("Sampling rate set to: %d Hz\n", param);
      else
      {
         close(dsp);
         printf("Error setting sampling rate %d\n", param);
         return -3;
      }

      // 24bit stereo sinus generation; default frequency = 1000 Hz, default level = 0.5 (half scale)
      int    i, L, R;
      double dt = 1.0/param;
      double f  = (argc > 1) ? strtod(argv[1], NULL) : 1000.0;
      double l  = (argc > 2) ? strtod(argv[2], NULL) : 0.5;
      int    d  = (argc > 3) ? (int)strtol(argv[3], NULL, 10) : 30;

      if (f < 0.0 || 20000.0 < f)
         f = 1000.0;

      if (l < 0.0 || 1.0 < l)
         l = 0.5;

      for (i = 0; i < d*param; i++)
      {
         L = R = lround(l*0x7FFFFF*sin(pi2*f * i*dt)) << 8;
         write(dsp, &L, 4);
         write(dsp, &R, 4);
      }

      close(dsp);
   }

   return 0;
}
Save this with as a file dsp-sinus.c
Compile it using clang dsp-sinus.c -lm -o dsp-sinus

Usage: dsp-sinus [freqency] [level] [duration]

Examples:
./dsp-sinus plays a sine with 1000 Hz at half level for 30 seconds.
./dsp-sinus 5000 0.3333 45 plays a sine with 5000 Hz at a level of 1/3 for 45 s.

If you really meant to change the sampling frequency instead of the frequencey of the resulting noise, then change the following lines to whatever you desire AND what stays within the specs of the sound device:
Code:
      // force a sampling rate of 48 kHz
      param = 48000;
PS: The actual wave form is defined by the sin() function, parameters and multipliers on the following line.
Code:
         L = R = lround(l*0x7FFFFF*sin(pi2*f * i*dt)) << 8;
For example L = R = lround(l*0x7FFFFF*exp(-5.0/d * i*dt)*cos(pi2*f * i*dt)) << 8; would play a decaying noise over the defined duration (see attached file).
 

Attachments

Back
Top