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

CWinamp - more than just a Winamp2 API wrapper

By , 13 Mar 2003
 

Sample Image - winamp2api.gif

Introduction

Prorgammers are lazy, we all know that. Whenever we come across some API we want to make a wrapper for it in order to use it more easily. Microsoft wrote its "Foundation Classes", and every day someone posts an article about his own new wrapper class here on CodeProject.com. I have also decided to do so.

I needed to control my Winamp2 from another app, and after doing some research if finally found the fine Winamp2 API documentation. But after a few minutes I was tired to type all those SendMessages with a number as parameter that no one on earth would want to remember. After playing around with the official Winamp2 API I had to discover that the API doesn't provide some handy functions such like getting the current volume. So what now? Well, I began writing the wrapper. Soon I started Spy++ to get access to some handy but undocumented API calls, e.g. reading the volume or generating a HTML playlist. Woohoo! Now I felt it was time to share my work with you :-)

My main goal was to keep the wrapper simple as possible, so that even beginners would have no problems using it. The result is one single header-file that needs to be included to your workspace. No more! Now let's start, I'll explain the most important steps and functions.

Using the winamp2 wrapper class

Using this class is easy as can be. Just #include "winamp2.h" to your workspace. Next, create our wrapper class variable somewhere in your project:

CWinamp amp // create the wrapper variable named "amp"

Before you can use all the functions you need to FindWinamp(). For most people it safe enough to call this function without any parameters. Winamp can be opened with a different window class but the standard "Winamp v1.x" by starting it with the parameter /CLASS. But most people don't do so, so you don't have to worry about the FindWindow() parameter. The function returns true, if Winamp has been found. Now you are ready to use all the nifty functions.

There are so many functions that I don't want to explain them all in detail. These are probably the most important ones:

void    Previous();
void    Next();
void    Play();
void    Pause();
void    Stop();
const char* GetCurrentTitle()

Should be self-explanatory I hope :-) So if you want to go to the next track just call the Next() function like that:

CWinamp amp;
if(amp.FindWinamp())
{
    // Winamp has been found, play next track
    amp.Next();
}

The functions all have names that explain what they do, e.g. TrackGetPositionMSec(). This returns the track's current postion in milliseconds. There are three functions that need to be discussed a bit more in detail, but acutally they are easy to use as well. The functions are as follows:

int GetVolume();
void SetVolume(int volume);
int EQGetData(int band);
void EQSetData(int band, int value);
int EQGetPreampValaue();

The GetVolume() functions returns a value between 0 and 255. 0 means volume turned off completely (i.e. silent), whereas 255 means full volume. This way you can pass a value of 0 to 255 to SetVolume(). EQGetData(int band) takes a value of 0-9 as parameter, representing one of the ten bands of the equalizer. It returns a value between 0 and 63. 0 means a value of -20 dB and 63 a value of +20 dB. Consider this, if you want to represent the value by e.g. a CSliderCtrl, make sure to set its range like that: SetRange(0, 63). To set the value of a specific band, call EQSetData(). The first parameter is the band (0-9), the second the value in the range from 0-63.

Note: For some reason, Winamp does NOT refresh the equalizer bars during runtime, but the changes are applied. You need to RestartWinamp() to make the changes visible in the equalizer. Blame the programmers of winamp ;-) EQGetPreampValaue() also returns the preamp value in the range of 0-63.

Conclusion

I'd say this is everything you need to know in order to understand the class. If you have any futher questions, feel free to post a comment or send me a mail. The class has been successfully tested with Winamp 2.81

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Greeeg
Germany Germany
Member
No Biography provided

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   
Questioncan u guide me i am new for mixer controlmemberrajneshmalik30 Aug '07 - 2:23 
hi
i want to play two sound file same time and want to listen the sound of these file
on separtly on left and right speaker,means sound of one file should play on left
speaker and second file should play on right speaker
 
thank u in advance
malik
GeneralBuild problemsmemberrich_dave22 Feb '06 - 6:51 
Hi,looks like a great bit of software you have wrote here, It will help solve a little problem i have perfectally.
 
Only problem is when i try to include the header file I get four error messagues
 
f:\uni\project\code\winamp\winamp\winamp2.h(17) : error C2664: 'FindWindowW' : cannot convert parameter 1 from 'const char [12]' to 'LPCWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

 
All are similar to one above.
 
I think I probably just showing how much of a noob I am here!
 
Any help would be much appreciated
 
compiling in visual studio 2005 express edition on Windows XP
 
Thanks
 
Rich
GeneralRe: Build problemsmemberGreeeg22 Feb '06 - 23:44 
When you're using a unicode build then use TCHAR instead of char, this will ensure unicode compatibility Smile | :)
 
regards
QuestionIs it possible to set the output device?memberHyun-Chul Lee6 Nov '05 - 20:09 
Hi,
 
I am trying to write a program for a multichannel(8 mono channels) soundcard.
It must be able to play 8 mp3 files on each channel at the same time.
In other words, I need 8 mp3 players running at the same time.
But it seems like this library controls one winamp program.
I am looking though the documentation, however, cannot find any comment about this problem.
Can you tell me if it is possible to make this kind of application
with your winamp wrapper library?
Thank you.
 
kennyd

 
kennyd
AnswerRe: Is it possible to set the output device?memberGreeeg7 Nov '05 - 3:41 
Hello!
 
This class has been designed to only work with one open winamp instance.
However, you have two options:
 
1.
Create each of your winamp instances with a different window class, i.e start it with "Winamp.exe /class=myinstance1" and so on. You can automate this by using ShellExecute(NULL, "open", "C:\\Program Files\\Winamp\\Winamp.exe", "/class=myinstance1", NULL, 0);
Then you can find all Winamp main windows with CWinamp::FindWinamp(myinstance1) and so on.
 
Unfortunately the CWinamp class only searches for ONE equalizer and playlist window. So you basically need winamp to start with directly opening a file you specified by your own. Dunno if this is possible with a parameter.
 
2.
Second option would be to rewrite the code in the winamp class.
That would mean to change the FindWindow calls into EnumWindows calls to enumerate all open Winamp main/equalizer/playlist windows. Next, you'd need to do a GetWindowThreadProcessId(HWND hwnd,DWORD &processId); on your found winamp window handles and associate all the different windows to the appropriate process handles.
Quite a bit work, but that should do the job.
 
regards
Generalwhy din't you. .. .membereuacela27 Dec '04 - 11:19 
wrote a plug-in for winamp it would have been much easier but anyway the program is good
 
gabby
GeneralMissing: EQ On-offmembermg123427 Nov '04 - 16:48 
All the EQ sliders reflect their values in Winamp.
Just curious why the EQ on/off and Auto buttons don't
work from your program's side. Did Winamp forget to
enable them?
 
Nice work on this project!

GeneralRe: Missing: EQ On-offmemberGreeeg29 Nov '04 - 2:10 
As far as I remember (long time ago since I posted this article Big Grin | :-D ) I did not implement a "enable/disable EQ" function. But actually it should be possible to implement this with two simple FindWindow/SendMessage calls. Perhaps I will find a bit time to do this, but I'm lacking time right now.
 
regards
GeneralRe: Missing: EQ On-offsussAnonymous30 Nov '04 - 20:57 
Thanks, Greeeg. Please let me know if you get
anywhere with this. How did you find the button
numbers to begin with?

GeneralRe: Missing: EQ On-offmemberGreeeg1 Dec '04 - 8:03 
Part of them are documented in the NSDN, the rest I found out by using Spy++. You can intercept the messages a window/application receives as well find out which (hidden) windows it has
 
Same I did with the equalizer just now.
 
You can write a function for testing purposes:
 
void ToggleEQ() 
{ 
   if(wndEQ) 
   { 
       PostMessage(wndEQ, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(25,25));
       PostMessage(wndEQ, WM_LBUTTONUP, 0, MAKELPARAM(25,25));
    }
}
 
You should be at least able to toggle the EQ on/off this way, but I havent tested it yet, sorry.

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 14 Mar 2003
Article Copyright 2003 by Greeeg
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid