Noise Generator in Linux

Source: Internet
Author: User
Linux noise generator-general Linux technology-Linux programming and kernel information. For details, see the following. It may be useful to get the white noise generator program under linux and share it. Next, solve the driver problem.

# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Define BUFSIZE 88200 // 100 ms wav is generated every time
# Define count 100 // 10 s data is produced


Char magic [] = {0x52,0x49,0x46,0x46}; // "wave"
Char middle [] = {0x57,0x41,0x56,0x45,0x66, 0x6D, 0x74,0x20,
0 x, 0 x, 0 x, 0 x, 0 x, 0 x,
0x44, 0xAC, 0 x, 0 x, 0x10, 0xB1, 0 x, 0x00,
0 x, 0 x, 0 x, 0x64, 0x61}; // other frame data

Short rxbuf [BUFSIZE];

Void write_wavheader (int fd, int data_len)
{
Int tmp;
Write (fd, magic, 4 );
Tmp = data_len + 36;
Write (fd, (char *) & tmp, 4 );
Write (fd, middle, 32 );
Tmp = data_len;
Write (fd, (char *) & tmp, 4 );
}




Int main (int argc, char ** argv)
{

Int out_fd;
Int I, j, realsize;
Int output;
Char * filename;

Filename = "/tmp/out.wav ";
If (out_fd = open (filename, O_RDWR | O_CREAT | O_TRUNC), 0666) <= 0)
{
Printf ("Can't open output file! Errno = % d \ n ", errno );
Return;
}
Memset (rxbuf, 0, BUFSIZE * sizeof (short ));
Write_wavheader (out_fd, count * BUFSIZE );
For (I = 0; I {
For (j = 0; j {
Rxbuf [j] = (short) (32760.0 * rand ()/(RAND_MAX + 1.0 ));
If (rxbuf [j] % 2 = 0)
Rxbuf [j] * =-1;
}

If (realsize = write (out_fd, rxbuf, BUFSIZE * sizeof (short )))! = BUFSIZE * sizeof (short ))
Printf ("ERROR: write (% s) return count: 0x % d, errno: % d \ n", filename, realsize, errno );
}

Printf ("\ nRecord finished. \ n ");
Return 0;
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.