Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

High quality graph/waveform display component in C#

Tags:

c#

waveform

I'm looking for a fast, professionally looking and customizable waveform display component in C#.

I'm wanting to display mainly real-time audio waveforms (fast!) in both time and frequency domain. I would like the ability to zoom, change axis settings, display multiple channels, customize the feel and colors etc...

Anybody knows of anything, whether commercial or not?

Thank you!

Diego

like image 317
user245015 Avatar asked Jan 11 '10 13:01

user245015


3 Answers

I bumped into a code project awhile ago that was doing this.

Check out http://www.codeproject.com/KB/miscctrl/GraphComponents.aspx it may be what you are looking for to do real-time graphing in .net

like image 136
Tj Kellie Avatar answered Nov 17 '22 06:11

Tj Kellie


as far as i know, national instrument has some cool control, but it's not free.

http://sine.ni.com/psp/app/doc/p/id/psp-317

free ones:

http://www.codeproject.com/KB/audio-video/wavecontrol.aspx

like image 2
Benny Avatar answered Nov 17 '22 06:11

Benny


Based on Illaya's code:

public void CreateWaveForm(string audioFilePath, string audioWaveFormFilePath)
    {
        try
        {
            int bytesPerSample = 0;
            using (NAudio.Wave.Mp3FileReader reader = new NAudio.Wave.Mp3FileReader(audioFilePath, wf => new NAudio.FileFormats.Mp3.DmoMp3FrameDecompressor(wf)))
            {
                using (NAudio.Wave.WaveChannel32 channelStream = new NAudio.Wave.WaveChannel32(reader))
                {
                    bytesPerSample = (reader.WaveFormat.BitsPerSample / 8) * channelStream.WaveFormat.Channels;
                    //Give a size to the bitmap; either a fixed size, or something based on the length of the audio
                    using (Bitmap bitmap = new Bitmap((int)Math.Round(reader.TotalTime.TotalSeconds * 40), 200))
                    {
                        int width = bitmap.Width;
                        int height = bitmap.Height;

                        using (Graphics graphics = Graphics.FromImage(bitmap))
                        {
                            graphics.Clear(Color.White);
                            Pen bluePen = new Pen(Color.Blue);

                            int samplesPerPixel = (int)(reader.Length / (double)(width * bytesPerSample));
                            int bytesPerPixel = bytesPerSample * samplesPerPixel;
                            int bytesRead;
                            byte[] waveData = new byte[bytesPerPixel];

                            for (float x = 0; x < width; x++)
                            {
                                bytesRead = reader.Read(waveData, 0, bytesPerPixel);
                                if (bytesRead == 0)
                                    break;

                                short low = 0;
                                short high = 0;
                                for (int n = 0; n < bytesRead; n += 2)
                                {
                                    short sample = BitConverter.ToInt16(waveData, n);
                                    if (sample < low) low = sample;
                                    if (sample > high) high = sample;
                                }
                                float lowPercent = ((((float)low) - short.MinValue) / ushort.MaxValue);
                                float highPercent = ((((float)high) - short.MinValue) / ushort.MaxValue);
                                float lowValue = height * lowPercent;
                                float highValue = height * highPercent;
                                graphics.DrawLine(bluePen, x, lowValue, x, highValue);
                            }
                        }

                        bitmap.Save(audioWaveFormFilePath);
                    }
                }
            }
        }
        catch
        {
        }
    }
like image 2
Daniël Teunkens Avatar answered Nov 17 '22 07:11

Daniël Teunkens