Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 20th, 2006, 12:54 PM   #1
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Everyone wants a challenge...

...but few have the energy or gonads or whatever to respond to those occassionally placed on the forum. They'd rather bitch about boredom.

Maybe you'd like to make your own visualizations for Windows Media Player (or skins, or whatever). You build this as a .dll. There is a lot of code involved, since the implementation uses a com object and has a lot of api stuff. The good news is that you can get a wizard when you download the WMP SDK. The wizard writes almost all the code you need. As a matter of fact, it writes a working visualization with two presets, Bars (a frequency representation) and Scope (an amplitude representation). They're not very interesting. At any rate, all you need to really write is the 'render' method and adjust your resource names and quantities.

I wrote a visualization called Mood with three presets, Spikes, Mood, and Scope. The Spikes preset divides the frequency samples into three sections: bass, midrange, and treble. The amplitude of the various frequency samples determine the length of the spike. Spikes in the bass range are red, the midrange is blue, and the treble range is yellow. It looks like this:

The Mood preset draws the spikes according to the amplitude of the signal. Their color is controlled by the relative amounts of energy in the three filtered bands. The color is RGB (bass, treble, mid). You will note that the code for this preset has a lot of quibbles in it for boost and for where the cutoffs are placed to form the bands. My intent is to analyze the frequency sample and derive the proper settings for a good equalization. These two images are Conway Twitty singing 'The Rose'. They are grabbed at the beginning, when the vocal is quiet, and just words; and at the finale.


The Scope preset is just the preset that the wizard provided:

This was done with VC++ 6.0. You can get the wizard for .NET, also. Some variable definitions may look out of place. VC++ 6.0 is not ANSI compliant in many respects, including dropping variables properly when they (supposedly) go out of scope.
The render method:
STDMETHODIMP CTestViz::Render(TimedLevel *pLevels, HDC hdc, RECT *prc)
{
    // Fill background with black
    HBRUSH hNewBrush = ::CreateSolidBrush (0);
    HPEN hNewPen = ::CreatePen (PS_SOLID, 0, m_foreColor);
    HPEN hOldPen= static_cast<HPEN>(::SelectObject (hdc, hNewPen));
    HPEN hRedPen = ::CreatePen (PS_SOLID, 0, RED);
    HPEN hYellowPen = ::CreatePen (PS_SOLID, 0, YELLOW);
    HPEN hBluePen = ::CreatePen (PS_SOLID, 0, BLUE);

    ::FillRect( hdc, prc, hNewBrush );
    
    // draw using the current preset
    double pi = 3.1415926535;
    int height = prc->bottom - prc->top;
    int width = prc->right - prc->left;
    double hop, jump;
    if (width > SA_BUFFER_SIZE)
    {
        jump = static_cast <double> (width / SA_BUFFER_SIZE);
        hop = 1.0;
    }else{
        jump = 1.0;
        hop = static_cast <double> (SA_BUFFER_SIZE / width);
    }
    double vSize = height / 256.0f; // maximum value of signal
    // Walk through the frequencies until we run out of levels or drawing surface.
    int barWidth = 4;
    int shift = barWidth/2;
    int xoffset = width/2;
    int yoffset = height/2;
    int bassCut = width / 5;
    int midCut = width * 3 / 5;

    switch (m_nPreset)
    {
        case PRESET_SPIKES:
        {
            // This preset displays the energy in each frequency sample.
            // The color changes at the defined point of a shift from bass
            // to midrange, and again between midrange and treble.
            for (int x = 0; x < width; x += barWidth)
            {
                int index = static_cast <int> (x * hop);
                if (x < bassCut) ::SelectObject (hdc, hRedPen);
                else if (x > midCut) ::SelectObject (hdc, hYellowPen);
                else ::SelectObject (hdc, hBluePen);
                int y = static_cast<int>(vSize * pLevels->frequency [0][index] / 2);
                for (int z = 0; z < shift; z++)
                {
                    ::MoveToEx (hdc, z+xoffset+x-shift, prc->bottom-yoffset-y, NULL );  
                    ::LineTo (hdc, z+xoffset-x-shift, prc->bottom-yoffset+y);
                    ::MoveToEx (hdc, z+xoffset-x-shift, prc->bottom-yoffset-y, NULL );  
                    ::LineTo (hdc, z+xoffset+x-shift, prc->bottom-yoffset+y);
               }
            }
        }
        break;

        case PRESET_MOOD:
        {
            // Draw a radial bar for each adjusted sample
            // Set the color to the energy content of the frequencies by
            // setting the red value to the average of the values below the bass cut.
            // do the same for the midrange (blue) and treble (green).
            // X offset = cos angle * waveform height.
            // Y offset = sin angle * waveform height.
            // Angle increment = 2PI (radians in a circle) / width (number of samples).
            double angleInc = 2*pi/width;
            int j;
            int bass = 0;
            int mid = 0;
            int treble = 0;
            for (j = 0; j < SA_BUFFER_SIZE; ++j)
            {
                int fValue = pLevels->frequency [0][j];
                if (j < bassCut) bass += fValue;
                else if (j > midCut) treble += fValue;
                else mid += fValue;
            }
            int bassBoost = 4;
            int midBoost = 4;
            int trebleBoost = 4;
            bass /= bassCut;
            bass *= bassBoost;
            bass = min (bass, 255);
            mid /= midCut-bassCut;
            mid *= midBoost;
            mid = min (mid, 255);
            treble /= SA_BUFFER_SIZE-midCut;
            treble *= trebleBoost;
            treble = min (treble, 255);
            HPEN hfreqPen = ::CreatePen (PS_SOLID, 0, RGB (bass, treble, mid));
            ::SelectObject (hdc, hfreqPen);
            for (int i = 0; i < width; ++i)
            {
                double h = vSize * pLevels->waveform [0][static_cast <int> (i * hop)];
                int x = static_cast<int>(std::cos (i*angleInc) * h);
                int y = static_cast<int>(std::sin (i*angleInc) * h);
                y /= 2;
                x /= 2;
                ::MoveToEx (hdc, prc->left + xoffset, prc->bottom - yoffset, NULL );  
                ::LineTo (hdc, prc->left+xoffset+x, prc->bottom-yoffset-y);
            }
            if (hfreqPen) ::DeleteObject (hfreqPen);
        }
        break;

        case PRESET_SCOPE:
        {
            // Walk through the waveform data until we run out of samples or drawing surface.
            int y = static_cast<int>(((prc->bottom - prc->top)/256.0f) * pLevels->waveform[0][0]);
            ::MoveToEx( hdc, prc->left, y, NULL );  
            for (int x = prc->left; x < prc->right && x < (SA_BUFFER_SIZE-1); ++x)
            {
                y = static_cast<int>(((prc->bottom - prc->top)/256.0f) * pLevels->waveform[0][x - (prc->left - 1)]);
                ::LineTo(hdc, x, y); 
            }
        }
        break;
    }
        
    if (hNewBrush)
    {
        ::DeleteObject( hNewBrush );
    }

    if (hNewPen)
    {
        ::SelectObject( hdc, hOldPen );
        ::DeleteObject( hNewPen );
    }
    if (hRedPen) ::DeleteObject (hRedPen);
    if (hYellowPen) ::DeleteObject (hYellowPen);
    if (hBluePen) ::DeleteObject (hBluePen);
    
    return S_OK;
}
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 20th, 2006, 2:37 PM   #2
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
thats pretty cool stuff
__________________
Profanity is the one language that all programmers understand.

Check out my Blog <---updated Nov 30 2007!
Pizentios is offline   Reply With Quote
Old Sep 20th, 2006, 3:12 PM   #3
RobEasy
Programmer
 
RobEasy's Avatar
 
Join Date: May 2006
Location: The US duhhhhh!
Posts: 42
Rep Power: 0 RobEasy is on a distinguished road
I had no idea how people created vizualizations. Thank you Dawei for an intro into the craft. I'm going to do some searching online to see if one could make visualizations with C#. Thanx again!
__________________
Work Hard... Play Harder!
RobEasy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Uman's WEEKEND CHALLENGE uman Coder's Corner Lounge 34 Aug 5th, 2008 10:25 PM
Little Challenge For A Bored Programmer Sane Coder's Corner Lounge 20 May 5th, 2006 7:11 AM
Crazy Challenge =D Ooble Coder's Corner Lounge 6 Sep 13th, 2005 2:46 PM
Challenge: How to make daily life better with programming? tempest Coder's Corner Lounge 53 Jun 17th, 2005 3:37 AM
Weekend Challenge theduck Community Announcements and Feedback 43 Jun 3rd, 2005 5:58 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:51 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC