Click here to Skip to main content
11,701,904 members (64,353 online)
Click here to Skip to main content

Draw into sound

, 21 Dec 2008 CPOL 53.2K 3.6K 81
Rate this:
Please Sign up or sign in to vote.
The article illustrates how to draw into a spectral representation of an audio file.

Introduction

The idea is to draw pictures into sounds. In particular, draw pictures into the spectral representation of the sound. A sound can be represented in many ways. In particular, it can be drawn with Amplitude-Time representation (Waveform) and Frequency-Time representation (Spectrum). The first shows the amplitude of the sound in time:

The X-axis is the time while the Y-axis is the amplitude of the sound. The next kind of view shows the audio signal as components of different frequencies:

Here, the X-axis is time while the Y-axis is the frequency. In particular, lighter colors (white) means higher value of the component of the specific frequency, while darker colors (black) means a lower component. Here, it’s possible to see how the sound changes in time and frequency.

The Fast Fourier Transform

The Fourier Transform is an operation that transforms a signal (in our case, the audio signal) into its frequency domain representation. So, it is possible to see which frequencies are present in the input signal. The FFT is an algorithm that computes the Fourier Transform efficiently.

The inverse operation (Inverse Fast Fourier Transform) takes data from the frequency domain and gives the values in the time domain. We can use this algorithm to draw a sound. Just imagine the draw as an input like if it was the spectral representation of the signal. Then, we apply the IFFT algorithm, and we retrieve the audio waveform which is used to create the output wave file.

The Program

The blackboard is the frequency-time representation of the audio signal. It is possible to paint into it using the color of the bar on the left side of the start button. Clicking the bar, it’s possible to change the color. The best results can be obtained using darker colors (like dark gray). When you click the Start button, the IFFT algorithm begins to compute data and the output audio file is generated.

Just draw something:

We can view the results with programs like Cooledit.

Fig6.jpg

The Code

The program is written in C#. We use two fundamental libraries here:

  • Wave File Library written by Garrett Hoofman. The library has been used to produce the Wav output file.
  • A library that implements the IFFT algorithm.
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WaveLibrary;

namespace Img2Wav
{
    class Core_Img2Wav
    {
        
        private const double MAX_DATA = +50;
        private const double MIN_DATA = -50;

        private const String NoInputBitmap = "No input bitmap";

        public Bitmap InputBitmap { get; set; }
        public WaveFile OutputWav { get; set; }

        public void Start()
        {
            int NumSamples = InputBitmap.Width * InputBitmap.Height;
            byte[] Samples = new byte[NumSamples];
            OutputWav = new WaveFile(1, 16, 44000);
            if (InputBitmap == null) throw new Exception(NoInputBitmap);
            double[] data = new double[InputBitmap.Height];

            int w = 0;
            for (int i = 0; i < InputBitmap.Width; i++) 
            {
                for (int j = 0; j < InputBitmap.Height; j++)
                {
                    Color C = InputBitmap.GetPixel(i,j);
                    data[j] = (C.R + C.G + C.B ) / 3;
                }

                FFT_Img2Wav.inverse(data);
                                
                for (int x = 0; x < data.Length; x++)
                {
                    Samples[w] = (byte)(MAX_DATA * data[x]);
                    w++;
                }
            }

            OutputWav.SetData(Samples,NumSamples);
        }
    }
}

License

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

Share

About the Author

evol76
Software Developer (Senior)
Italy Italy
Angelo Gattuso graduated in November 2003 from "Politecnico di Torino" in Software Engineering. He is a Software Engineer with experience in projecting and developing Businness Applications. Areas of expertise include ASP .Net design and develompemnt of three-tier applications.

He has interests in music, reading and cooking even if he prefers eating.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Bill SerGio Jr.23-Apr-15 4:28
memberBill SerGio Jr.23-Apr-15 4:28 
NewsC# Wave Library Pin
Member 1108725116-Sep-14 8:34
memberMember 1108725116-Sep-14 8:34 
QuestionHow to see the frequency Pin
Member 859082926-Jun-12 16:08
memberMember 859082926-Jun-12 16:08 
GeneralMy vote of 5 Pin
winxpdll11-Sep-11 10:10
memberwinxpdll11-Sep-11 10:10 
GeneralMy vote of 5 Pin
abdillahi26-Apr-11 23:08
groupabdillahi26-Apr-11 23:08 
GeneralMy vote of 4 Pin
Lucian-aSterX22-Feb-11 22:29
memberLucian-aSterX22-Feb-11 22:29 
GeneralGreat, but has some bugs Pin
M$VBC++27-Dec-10 4:33
memberM$VBC++27-Dec-10 4:33 
GeneralMy vote of 5 Pin
victorbos31-Jul-10 7:33
membervictorbos31-Jul-10 7:33 
GeneralSimply fascinating Pin
victorbos31-Jul-10 7:03
membervictorbos31-Jul-10 7:03 
GeneralLoss "WaveLibrary" in package Pin
kenj_huang010317-Jun-10 0:12
memberkenj_huang010317-Jun-10 0:12 
GeneralRe: Loss "WaveLibrary" in package Pin
victorbos31-Jul-10 6:54
membervictorbos31-Jul-10 6:54 
GeneralRe: Loss "WaveLibrary" in package Pin
filmee2426-Apr-14 1:22
memberfilmee2426-Apr-14 1:22 
GeneralTerrible article dude! Pin
TV Mogul16-Jan-10 13:10
memberTV Mogul16-Jan-10 13:10 
GeneralRe: Terrible article dude! Pin
evol7612-Mar-10 10:31
memberevol7612-Mar-10 10:31 
GeneralRe: Terrible article dude! Pin
victorbos31-Jul-10 7:11
membervictorbos31-Jul-10 7:11 
GeneralRe: Terrible article dude! Pin
TV Mogul31-Jul-10 12:37
memberTV Mogul31-Jul-10 12:37 
READ WHAT I SAID,

The article is terrible BECAUSE the sample code doesn't work!

Post the code that is the first image that is a spectrograph of a wav file after a FFT.... and then you will a nice article... but as it is, it is terrible!

If you write an article, then post code that works for your sample!

I am no genius... just a man who likes to support his family .... NO programmer should ever work for less than $55 an hour and if any company offers you less tell them to go and... !@#$%^&*(

Bill
http://www.KabbalahCode.com

GeneralSpectrogram Pin
Patt Rojazz5-Nov-09 7:35
memberPatt Rojazz5-Nov-09 7:35 
GeneralRe: Spectrogram Pin
evol7612-Mar-10 10:33
memberevol7612-Mar-10 10:33 
GeneralBetter Image Tools Pin
mkane726-Sep-09 12:59
membermkane726-Sep-09 12:59 
GeneralGood Idea! Pin
kmslove26-Jun-09 5:15
memberkmslove26-Jun-09 5:15 
GeneralVery Funny Idea! Pin
johannesnestler21-Dec-08 21:58
memberjohannesnestler21-Dec-08 21:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150819.1 | Last Updated 21 Dec 2008
Article Copyright 2008 by evol76
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid