15,112,232 members
1.00/5 (1 vote)
See more:
I'm writing an article about that with endless time if we run random frequencies we would get a sensible audio, as seen below I made for random images (video) and included this code in the article to explain my point, also would you please teach me how to produce for example a basic sound beep that goes from a high pitch to a low pitch or the opposite?

What I have tried:

```private void Timer1_Tick(object sender, EventArgs e)
{
for (int x = 0; x < 400; x++)
for (int y = 0; y < 400; y++)
{
g.DrawRectangle(new Pen(Color.FromArgb(255,
RN.Next(256),
RN.Next(256),
RN.Next(256))),
x,y,
1,1);
}
}```

I don't think beep will do the job of sensible audio like human speech.

In a 1 ms timer tick:

```using System.Runtime.InteropServices;
[DllImport("kernel32.dll")]
public static extern bool Beep(int freq, int duration);

Beep.Play(RN.Next(MAX_HZ),1);```

Does it do the job? if yes, as I don't know about sound, what is the range frequency in Hz I should run.
Posted
Updated 22-May-19 9:03am
v5

## Solution 2

I am sure there are mathematical ways to produce the sound file you are requesting. But something like this should work.

Increasing the pitch (as an example, decreasing pitch will just be the same data backwards) basically means the frequency needs to be increasing as the file is played. Most likely you would want a sin-wave for your audio as it sounds a bit nicer, but if you just want really simple code, stick to square wave.

So loop over the length of your wave file data buffer.
Typially you would use `sin(x * <frequencydefiningconstant>) * <amplitude>` where x is the position in the audio buffer. If you change the value in sin to a non-linier expression, you will get variation in the pitch. so try `sin(x^y * <frequencydefiningconstant>) * <amplitude>` (y should be really close to 1, as samples come pretty fast in a wave file). Not sure this is the best result, you can play with other non-linier functions... or wait and hope someone comes with a better answer.

I would expect a Fourier transforms is the mathematical right way to do it as it can transform between the frequency domain (where you can specify a linier increase/decrease) and the time domain (your sample index in the wave file). If this is the right approach for your paper probably depends on the audience.
john1990_1 22-May-19 13:43pm

What class or reference or Nugget should I use to generate an audio?
lmoelleb 22-May-19 13:51pm

No idea. I needed it back in the .NET 1.0 days, so I had to write my own, I just googled the format. PCM (uncompressed). Maybe NAudio can do it, not sure.
john1990_1 22-May-19 13:52pm

Thanks, please see updated question.

## Solution 3

Not a solution, so much as a suggestion. If you mean music when you say "sensible audio", I'd suggest generating random integer values between 0 and 127, using that value as if it were a MIDI note, then looking up the frequency value of that note via a lookup table of some sort. EX: MIDI 60 = Middle C = 261.6 Hz

That's for regular "Western" diatonic music. There are others.

As far as playing the audio, look at public static void Beep (int frequency, int duration) for the most basic way of doing it. There are better ways, using the computers audio features rather than the Beep.
john1990_1 22-May-19 13:52pm

Thanks, please see updated question.

## Solution 1

v2
john1990_1 21-May-19 12:06pm

I knew about the monkey thing before, but how is this related to the second link? also, any info about how to generate audio?
john1990_1 22-May-19 13:52pm

Thanks, please see updated question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 280 Richard MacCutchan 105 Richard Deeming 70 Maciej Los 60 Omar ice killerjj 25
 OriginalGriff 4,841 Richard MacCutchan 2,293 Richard Deeming 1,455 CHill60 1,128 CPallini 745

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900