Click here to Skip to main content
Click here to Skip to main content

Draw into sound

By , 21 Dec 2008
 

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)

About the Author

evol76
Software Developer (Senior)
Italy Italy
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow to see the frequencymemberMember 859082926 Jun '12 - 16:08 
Hi I was using this and was wondering if there will be updates for being able to see the frequencies?
GeneralMy vote of 5memberwinxpdll11 Sep '11 - 10:10 
Thank you. Excellent!
GeneralMy vote of 5groupabdillahi26 Apr '11 - 23:08 
Very helpful ! Thanks
GeneralMy vote of 4memberLucian-aSterX22 Feb '11 - 22:29 
It would be nice if you could load an image and then generate the sound from it Big Grin | :-D
GeneralGreat, but has some bugsmemberM$VBC++27 Dec '10 - 4:33 
Hey guys, you may say whatever you want, but this article is great. Still does it have some issues I encountered while using it:
a) It takes only the bottom side of the input image and x-axis-mirroredly puts the upper half of the input image right on top of it.
b) It somehow makes the sound file 2 times as long as it should be
c) It can take only a special amount of pixels per column without clipping the audio
All these can be fixed, I think, and I can live with a and c, but b always requires me to trim the end of the file in Cooledit (nowadays: Adobe Audition).
Although this article is quit old, does anyone know how to fix issue b?
Thank you and once again - great article, especially the idea fascinated me ;D No one would have the idea of looking at the sound file in the spectral view in order to see something useful^^
GeneralMy vote of 5membervictorbos31 Jul '10 - 7:33 
Very clever and novel. Good stuff!
GeneralSimply fascinatingmembervictorbos31 Jul '10 - 7:03 
I have no idea why some people are bitching and moaning and generally being rude. I think what you came up with is truly novel, at least to me, and I appreciate your contribution very much. Likewise Garrett Hoofman's Wave Library is really good, from a learning point of view.
 
(For those who could not find the library easily try this link: http://www.visionsofafar.com/dablog/category/Wave-File-Library.aspx[^])
 
This rates a 5 IMHO. I am sure you were able to put it to some equally clever use. If you can share it, some of us plebes would appreciate it. Thanks in advance.
 
Cheers! Rose | [Rose]
GeneralLoss "WaveLibrary" in packagememberkenj_huang010317 Jun '10 - 0:12 
Hi,
 
I can't compiler the package.
The first problem is loss "WaveLibrary" reference.
 
Kenj
I am a software engineer.

GeneralRe: Loss "WaveLibrary" in packagemembervictorbos31 Jul '10 - 6:54 
You should be able to find it here http://www.visionsofafar.com/dablog/category/Wave-File-Library.aspx[^]
GeneralTerrible article dude!memberTV Mogul16 Jan '10 - 13:10 
Hi,
 
Your article is terrible... the sample code doesn't work and the spectrograph in the article is the sample code that would have been useful and you didn't share that code.
 
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!
 
Bill
 
http://www.KabbalahCode.com

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 21 Dec 2008
Article Copyright 2008 by evol76
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid