Sign in to follow this  
russellman128

SDL CDROM

Recommended Posts

russellman128    100
The code below works fine if you are using ALSA sound. If you are using OSS or PulsaAudio it does not work. I have tried putenv("SDL_AUDIODRIVER=dsp"); with dma, pulse, audio, oss, and others. Got any ideals. /////////////////////// CODE ////////////////////////// /* * Gary Russell (ISSW) * June 18 2009 * Using SDL libuary. * Music CD Player. * * When program starts - the cd plays. * When exit program - cd stops. * "Esc" or Click on the window "X" to exit. * "Space Bar" or Click on active window will * pause or resume. * "Tab" stops and ejects, but does not exit program. * "P" or "p" will play the cd. If it is not playing. */ #include <iostream> #include <stdlib.h> #include <SDL/SDL.h> #include <SDL/SDL_mixer.h> #include <SDL/SDL_cdrom.h> using namespace std; #define Screen_Width 100 #define Screen_Height 100 //If Bits_color is 0, it is treated as the current display bpp. #define Bits_color 0 #define Video_flages SDL_ANYFORMAT //#define Video_flages SDL_DOUBLEBUF | SDL_HWSURFACE //| SDL_FULLSCREEN SDL_Surface *screen = NULL; SDL_Surface *icon = NULL; SDL_Event event; SDL_CD *cdrom = NULL; char driver[] = "oss"; /////////////////////// FUNCTIONS ////////////////////////// void clean_up(void) { SDL_CDClose(cdrom); SDL_Quit(); } ////////////////////////////// //Setup SDL and anything else needing setup. bool setup(void) { //use to center the video box. call befor SDL_SetVideoMode(). // putenv("SDL_VIDEO_WINDOW_POS"); // putenv("SDL_VIDEO_CENTERED=1"); //This will put it at the uper left corner. putenv("SDL_VIDEO_WINDOW_POS=0,0"); //putenv("SDL_AUDIODRIVER=dsp"); //putenv("SDL_DISKAUDIOFILE=/home/admin/Temp/music.raw"); // system("export SDL_AUDIODRIVER"); if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) { cerr << "UNABLE TO INITIALIZE VIDEO: " << SDL_GetError() << endl; exit(1); } icon = SDL_LoadBMP("icon.bmp"); SDL_WM_SetCaption("Press (Esc) key to exit!", "Press (Esc) key to exit!"); SDL_WM_SetIcon(icon,NULL); screen = SDL_SetVideoMode(Screen_Width,Screen_Height,Bits_color,Video_flages); if (screen == NULL) { cerr << "Unable to set video mode: " << SDL_GetError() << endl; exit(1); } if(SDL_Init(SDL_INIT_CDROM) < 0 ) { cerr << "Couldn't initialize SDL: " << SDL_GetError() << endl; exit(1); } return true; } ////////////////////////////// //Opening the default drive bool open_def_cdrom(void) { bool error = false; CDstatus status; char *status_str; cdrom = SDL_CDOpen(0); if(cdrom == NULL) { fprintf(stderr, "Couldn't open default CD-ROM drive: %s\n", SDL_GetError()); exit(2); } status = SDL_CDStatus(cdrom); switch (status) { case CD_TRAYEMPTY: status_str = "tray empty"; error = true; break; case CD_STOPPED: status_str = "stopped"; break; case CD_PLAYING: status_str = "playing"; break; case CD_PAUSED: status_str = "paused"; break; case CD_ERROR:int trk = 2; status_str = "error state"; error = true; break; } //end switch(). printf("Drive status: %s\n", status_str); if(status >= CD_PLAYING) { int m, s, f; FRAMES_TO_MSF(cdrom->cur_frame, &m, &s, &f); printf("Currently playing track %d, %d:%2.2d\n", cdrom->track[cdrom->cur_track].id, m, s); }//end if(). return error; } ////////////////////////////// //Plays the whole cd. bool PlayCD(void) { //Change SDL_CDPlayTracks(cdrom, 0, 0, 0, 0); to //SDL_CDPlayTracks(cdrom, 0, 0, 1, 0); will play only //the first track. if(CD_INDRIVE(SDL_CDStatus(cdrom))) { SDL_CDPlayTracks(cdrom, 0, 0, 0, 0); } return true; } ////////////////////////////// ////////////////////////////// MAIN //////////////////////// int main() { bool ret,kstate; int quit = false; CDstatus status; ret = setup(); if(ret == true) { } kstate = false; open_def_cdrom(); PlayCD(); cout << "PLAY CD \n"; SDL_Delay(100); //main loop while(quit == false) { if(SDL_WaitEvent(&event) == 0) { cerr << "ERROR IN SDL_WaitEvent(): " << SDL_GetError() << endl; exit(1); } switch(event.type) { case SDL_ACTIVEEVENT: { }break; case SDL_KEYDOWN: { if(event.key.keysym.sym == SDLK_ESCAPE) { SDL_CDStop(cdrom); quit = true; } else if(event.key.keysym.sym == SDLK_SPACE) { if(kstate == false) { SDL_CDPause(cdrom); kstate = true; } else { SDL_CDResume(cdrom); kstate = false; } } else if(event.key.keysym.sym == SDLK_TAB) { SDL_CDStop(cdrom); SDL_CDEject(cdrom); } else if(event.key.keysym.sym == SDLK_p) { status = SDL_CDStatus(cdrom); if(status == CD_STOPPED) { PlayCD(); cout << "PLAY CD \n"; SDL_Delay(100); } } }//end SDL_KEYDOWN. case SDL_KEYUP: { }//end SDL_KEYUP. case SDL_MOUSEMOTION: { }break; case SDL_MOUSEBUTTONDOWN: { //if(event.key.keysym.sym == SDLK_SPACE) { if(kstate == false) { SDL_CDPause(cdrom); kstate = true; } else { SDL_CDResume(cdrom); kstate = false; } }break; case SDL_MOUSEBUTTONUP: { }break; case SDL_VIDEOEXPOSE: { }break; case SDL_VIDEORESIZE: { }break; case SDL_QUIT: { SDL_CDStop(cdrom); quit = true;} default: { }break; }//end switch(). };//end main loop. clean_up(); //clean up. return 0; }

Share this post


Link to post
Share on other sites
leeor_net    343
No, I don't know and part of the reason is because I can't be bothered to read your code because you haven't bothered to use 'code tags' as referenced in the first reply to your thread. You need to make sure that you follow appropriate guidelines and when someone makes a suggestion to you (e.g., "code tags are your friend") to heed them before making more demands. Nobody likes to respond to "it don't work!1! fix it 4 m3!!1!". I'm not trying to be a jerk just pointing out that there are good ways of asking questions and there are not so good ways of asking questions.

I think you're going to find much better help on the SDL newsboards that I linked to. I'd guess that most of the more experienced programmers here are familiar with Windows and there are far more linux developers browsing through the news groups. You're also likely to find help on linux specific forums as Linux has habit of having errors that generally don't happen on other platforms.

Share this post


Link to post
Share on other sites
russellman128    100


Linux SDL, SDL_cdrom.

The code below works fine if you are using ALSA sound. If you are using OSS or PulsaAudio
it does not work. I have tried

putenv("SDL_AUDIODRIVER=dsp"); with dma, pulse, audio, oss, and others. Got any ideals.

I belive tere is a function to set the cdrom audio driver. Jast as there is in SDL_image. The putenv("SDL_AUDIODRIVER=drivername"); will set the SDL_image audio driver to the deziared driver like directx. Although it is not need in SDL_mixer becouse SDL_mixer determens the bast driver for you. But SDL_cdrom does not. The code below will play a music cd in linux if you are using the ALSA audio driver. How do I get the code to determin the correct audio driver and set that driver in SDL_cdrom?

/////////////////////// CODE //////////////////////////


/*
* Gary Russell (ISSW)
* June 18 2009
* Using SDL libuary.
* Music CD Player.
*
* When program starts - the cd plays.
* When exit program - cd stops.
* "Esc" or Click on the window "X" to exit.
* "Space Bar" or Click on active window will
* pause or resume.
* "Tab" stops and ejects, but does not exit program.
* "P" or "p" will play the cd. If it is not playing.
*/


#include <iostream>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
#include <SDL/SDL_cdrom.h>

using namespace std;

#define Screen_Width 100
#define Screen_Height 100
//If Bits_color is 0, it is treated as the current display bpp.
#define Bits_color 0
#define Video_flages SDL_ANYFORMAT
//#define Video_flages SDL_DOUBLEBUF | SDL_HWSURFACE //| SDL_FULLSCREEN

SDL_Surface *screen = NULL;
SDL_Surface *icon = NULL;
SDL_Event event;
SDL_CD *cdrom = NULL;
char driver[] = "oss";

/////////////////////// FUNCTIONS //////////////////////////
void clean_up(void) {

SDL_CDClose(cdrom);
SDL_Quit();
}
//////////////////////////////
//Setup SDL and anything else needing setup.
bool setup(void) {

//use to center the video box. call befor SDL_SetVideoMode().
// putenv("SDL_VIDEO_WINDOW_POS");
// putenv("SDL_VIDEO_CENTERED=1");
//This will put it at the uper left corner.
putenv("SDL_VIDEO_WINDOW_POS=0,0");
//putenv("SDL_AUDIODRIVER=dsp");
//putenv("SDL_DISKAUDIOFILE=/home/admin/Temp/music.raw");
// system("export SDL_AUDIODRIVER");

if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
cerr << "UNABLE TO INITIALIZE VIDEO: " << SDL_GetError() << endl;
exit(1); }

icon = SDL_LoadBMP("icon.bmp");
SDL_WM_SetCaption("Press (Esc) key to exit!", "Press (Esc) key to exit!");

SDL_WM_SetIcon(icon,NULL);

screen = SDL_SetVideoMode(Screen_Width,Screen_Height,Bits_color,Video_flages);
if (screen == NULL) {
cerr << "Unable to set video mode: " << SDL_GetError() << endl;
exit(1); }

if(SDL_Init(SDL_INIT_CDROM) < 0 ) {
cerr << "Couldn't initialize SDL: " << SDL_GetError() << endl;
exit(1); }

return true;
}
//////////////////////////////
//Opening the default drive
bool open_def_cdrom(void) {
bool error = false;
CDstatus status;
char *status_str;

cdrom = SDL_CDOpen(0);
if(cdrom == NULL) {
fprintf(stderr, "Couldn't open default CD-ROM drive: %s\n", SDL_GetError());
exit(2); }

status = SDL_CDStatus(cdrom);
switch (status) {
case CD_TRAYEMPTY:
status_str = "tray empty";
error = true;
break;
case CD_STOPPED:
status_str = "stopped";
break;
case CD_PLAYING:
status_str = "playing";
break;
case CD_PAUSED:
status_str = "paused";
break;
case CD_ERROR:int trk = 2;
status_str = "error state";
error = true;
break;
} //end switch().

printf("Drive status: %s\n", status_str);

if(status >= CD_PLAYING) {
int m, s, f;
FRAMES_TO_MSF(cdrom->cur_frame, &m, &s, &f);
printf("Currently playing track %d, %d:%2.2d\n",
cdrom->track[cdrom->cur_track].id, m, s);
}//end if().

return error;
}
//////////////////////////////
//Plays the whole cd.
bool PlayCD(void) {

//Change SDL_CDPlayTracks(cdrom, 0, 0, 0, 0); to
//SDL_CDPlayTracks(cdrom, 0, 0, 1, 0); will play only
//the first track.
if(CD_INDRIVE(SDL_CDStatus(cdrom))) {
SDL_CDPlayTracks(cdrom, 0, 0, 0, 0); }

return true;
}
//////////////////////////////

////////////////////////////// MAIN ////////////////////////
int main() {
bool ret,kstate;
int quit = false;
CDstatus status;

ret = setup();
if(ret == true) { }

kstate = false;

open_def_cdrom();

PlayCD(); cout << "PLAY CD \n"; SDL_Delay(100);

//main loop
while(quit == false) {

if(SDL_WaitEvent(&event) == 0) {
cerr << "ERROR IN SDL_WaitEvent(): " << SDL_GetError() << endl;
exit(1); }

switch(event.type) {

case SDL_ACTIVEEVENT: { }break;
case SDL_KEYDOWN: {
if(event.key.keysym.sym == SDLK_ESCAPE) {
SDL_CDStop(cdrom); quit = true; }

else if(event.key.keysym.sym == SDLK_SPACE) {
if(kstate == false) {
SDL_CDPause(cdrom); kstate = true; }
else { SDL_CDResume(cdrom); kstate = false; }
}

else if(event.key.keysym.sym == SDLK_TAB) {
SDL_CDStop(cdrom);
SDL_CDEject(cdrom); }

else if(event.key.keysym.sym == SDLK_p) {
status = SDL_CDStatus(cdrom);
if(status == CD_STOPPED) {
PlayCD(); cout << "PLAY CD \n"; SDL_Delay(100); }
}

}//end SDL_KEYDOWN.
case SDL_KEYUP: { }//end SDL_KEYUP.
case SDL_MOUSEMOTION: { }break;
case SDL_MOUSEBUTTONDOWN: {

//if(event.key.keysym.sym == SDLK_SPACE) {
if(kstate == false) { SDL_CDPause(cdrom); kstate = true; }
else { SDL_CDResume(cdrom); kstate = false; }
}break;

case SDL_MOUSEBUTTONUP: { }break;
case SDL_VIDEOEXPOSE: { }break;
case SDL_VIDEORESIZE: { }break;
case SDL_QUIT: { SDL_CDStop(cdrom); quit = true;}
default: { }break;

}//end switch().

};//end main loop.

clean_up(); //clean up.
return 0;
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this