A simple music system example script ( wav, mp3, ogg ... )

Posted By: rayp

A simple music system example script ( wav, mp3, ogg ... ) - 03/27/14 11:26

And again me...

I made this example of an simple music - player system. It features volume fading between songs etc.
If u optimize something: POST!

Code:
// ----------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------
/* 27.03.2014
   ----------------------------------------------------------------------------------------------
   music - system (ms) example script         rayp 2014  free2use   forum:www.coniserver.net/ubb7
   ----------------------------------------------------------------------------------------------
   You can simply add more songs. CopyNPaste MS_SongX and the "if (_ms_song_newsong == X... -
   line" and count up :D see example MS_Song3
   Note: switching songs ingame : _ms_song_newsong = Number of newsong 0 for none ... thats it !
         call "_init_music_system();" once, and have fun !
         set _mm_music_on to 1 be4 calling !
   example needs: song1.mp3 and song2.mp3 placed in your main game directory ( main.c )
*/   
// ----------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------
STRING* MS_Song1      = "song1.mp3";                   // place mp3's in project's main folder
STRING* MS_Song2      = "song2.mp3";
//STRING* MS_Song3    = "song3.mp3";                   // and so on ...
// ----------------------------------------------------------------------------------------------
var _mm_music_on      =  1;                            // 0 = stop music
var _ms_song_newsong  =  1;                            // change this value to load new song nr X
var _ms_song_playing  =  0;                            // song which is playing right now
var _ms_song_handle   =  0;                            // handle to now playing song
var _ms_master_volume = 80;                            // master volume 4 song play
// ----------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------
void _init_music_system(){                             // global running music box function
   proc_mode          = PROC_GLOBAL; 	
   var _fade_to_sound = 0;
   var _volume        = 0;
   _ms_song_handle    = media_loop (MS_Song1, NULL, 0);
   while (1){
      if (!_mm_music_on)              { media_stop (_ms_song_handle); break; return; } // stop all
      if (_volume != _ms_master_volume) media_tune (_ms_song_handle, _ms_master_volume, NULL, 0);//changed vol?		
      if (_ms_song_playing != _ms_song_newsong && !_fade_to_sound){
         _fade_to_sound    = 1;
         _ms_song_playing  = _ms_song_newsong;
         while (_volume > 0){
            _volume -= time_step * 2;
            media_tune (_ms_song_handle, _volume, NULL, 0);
            wait (1);
         }
         media_stop (_ms_song_handle); 
         if (_ms_song_newsong == 1) _ms_song_handle = media_loop (MS_Song1, NULL, 0); 
         if (_ms_song_newsong == 2) _ms_song_handle = media_loop (MS_Song2, NULL, 0); 
       //if (_ms_song_newsong == 3) _ms_song_handle = media_loop (MS_Song3, NULL, 0); 	      
         while (_volume < _ms_master_volume){
            _volume += time_step * 2;
            media_tune (_ms_song_handle, _volume, NULL, 0);
            wait (1);
         }
         _fade_to_sound = 0;
      }		
      wait (1);
   }
}
// ----------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------
// end of this great contribution ... ifuwant give credits 2 rayp ... building a deathstar withit
// ----------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------

See manual
Quote:
media_play, media_tune, media_loop, media_stop
4 more Infos

Greets
Posted By: Random

Re: A simple ( mp3 ) music system example script - 03/27/14 15:23

Finally!
An exsample with all the functions!

Thanks!
Posted By: rayp

Re: A simple ( mp3 ) music system example script - 03/27/14 20:23

Your welcome!
Posted By: Realspawn

Re: A simple ( mp3 ) music system example script - 05/09/14 17:33

great contribution as always laugh
Posted By: Scifi

Re: A simple ( mp3 ) music system example script - 05/10/14 13:25

Share my very simple music player I wrote a long time ago because I thought it could be useful if one wants a lightweight but fully-featured music player without having to rewrite it. grin

Just plug the code in your projects and run - no additional files or dependencies required. And you can write a GUI that makes use of the functions, for example, one GUI button that use mpNext(), which will cycle to the next song in the list.

mpLoad(STRING *scan_path, STRING *scan_ext) must first be called to determine which directory to scan and the file extension, for example
Code:
mpLoad( "./sound/music/", "*.mp3" );



Header :
Code:
TEXT *mpPool = { strings ( 1000 ); } // Maximum of 1000 songs
int mpCount = 0, mpRandomize = 1, mpSongs, mpPauseMark;
var mpHandle;

var MainMenuMusicHndl;
void mpLoad(STRING *, STRING *);
void mpUnload();
void mpPrev();
void mpNext();
void mpPause();
void mpResume();
void mpPlay(STRING *);



Code:
/*
--------------------------------------------------
void mpLoad(STRING *scan_path, STRING *scan_ext)

Desc:

Returns: -
--------------------------------------------------
*/
void mpLoad(STRING *scan_path, STRING *scan_ext) {

STRING *_scan_path = str_create(scan_path); // Prevent modification of the original string

mpSongs = txt_for_dir(mpPool, str_cat(_scan_path,scan_ext) );
while(proc_status(txt_for_dir)) wait(1);

str_remove(_scan_path);

}

/*
--------------------------------------------------
void mpPlay(STRING *songName)

Desc:

Returns: -
--------------------------------------------------
*/
void mpPlay(STRING *songName) {

if(media_playing(mpHandle)) media_stop(mpHandle);
if(mpCount > mpSongs - 1 || mpCount < 0) return;

str_cpy(_mpSongTemp,songName);
str_trunc(_mpSongTemp, str_len( 4 )-1 );
str_cpy(mpSongTemp,PATH_MUSIC);
str_cat(mpSongTemp,songName);

str_for_num(_mpCount, mpCount); // strcpy is unnecessary here

mpHandle = media_play(mpSongTemp,NULL,VOL_MUSIC);

while(media_playing(mpHandle)) wait(1);
mpNext();

}

/*
--------------------------------------------------
void mpNext()

Desc:

Returns: -
--------------------------------------------------
*/
void mpNext() {

if(event_type == EVENT_RELEASE) return;

if(mpCount < mpSongs) {

if(mpRandomize) mpCount = (int) random(mpSongs);
else mpCount += 1;
mpPlay( (mpPool.pstring)[mpCount] );

}

}

/*
--------------------------------------------------
void mpPrev()

Desc:

Returns: -
--------------------------------------------------
*/
void mpPrev() {

if(event_type == EVENT_RELEASE) return;

if(mpCount >= 0) {

if(mpRandomize) mpCount = (int) random(mpSongs);
else mpCount -= 1;
mpPlay( (mpPool.pstring)[mpCount] );

}

}

/*
--------------------------------------------------
void mpPause()

Desc:

Returns: -
--------------------------------------------------
*/
void mpPause() {

if(event_type == EVENT_RELEASE) return;

if(media_playing(mpHandle)) {

media_pause(mpHandle);
mpPauseMark = 1;

}

}

/*
--------------------------------------------------
void mpResume()

Desc:

Returns: -
--------------------------------------------------
*/
void mpResume() {

if(event_type == EVENT_RELEASE) return;

if(mpPauseMark) media_start(mpHandle);

}

© 2024 lite-C Forums