[code/core] Some Music additions...

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
Durante
Posts: 65
Joined: Sun Oct 02, 2005 6:07 am
Location: Austria

[code/core] Some Music additions...

Post by Durante »

I just made some trivial additions to the Music object, here's the code in a zip (sound.h, sound.c, luasound.c):
http://peter.metaclassofnil.com/stuff/p ... update.zip

These are non-breaking changes (well, compared to some of my recent graphics stuff, everything is...) so they should be easy to add to the svn version. Note that only the bpm stuff is tested as of now, but I'll get to the other functions soon and I don't believe that there's an easy way to mess up such simple stuff ;).

For completeness, here are the added parts. I even documented them o_O
sound.h:

Code: Select all

/**
 * Sets the music's beats per minute.
 * 
 * @pre &#40;arg > 33 && arg < 240&#41;
 * @param arg is the number of bpm
 * @return the old bpm
 */
extern unsigned setMusicBPM&#40;unsigned arg&#41;;
/**
 * Gets the music's current beats per minute.
 * 
 * @return the bpm
 */
extern unsigned getMusicBPM&#40;&#41;;
/**
 * Gets the music's initial beats per minute.
 * 
 * @return the initial bpm of the current music file
 */
extern unsigned getMusicInitBPM&#40;&#41;;

/**
 * Sets the music's current position. All playing samples are cut off.
 * 
 * @pre &#40;arg < getMusicMaxPosition&#40;&#41;&#41;
 * @param arg is the position you want to set
 * @return the old position
 */
extern unsigned setMusicPosition&#40;unsigned arg&#41;;
/**
 * Gets the music's current pattern position.
 * 
 * @return the position
 */
extern unsigned getMusicPosition&#40;&#41;;
/**
 * Gets the music's last pattern position.
 * 
 * @return the last position of the current music file
 */
extern unsigned getMusicMaxPosition&#40;&#41;;

/**
 * Sets the music's positionto the current postion + 1.
 */
extern void nextMusicPosition&#40;&#41;;
/**
 * Sets the music's positionto the current postion - 1.
 */
extern void prevMusicPosition&#40;&#41;;
sound.c:

Code: Select all

unsigned setMusicBPM&#40;unsigned arg&#41; &#123;
	unsigned ret = musichandle->bpm;
	arg = &#40;arg<33&#41; ? 33 &#58; arg;
	arg = &#40;arg>240&#41; ? 240 &#58; arg;
	musichandle->bpm = arg;
	return ret;
&#125;
unsigned getMusicBPM&#40;&#41; &#123;
	return musichandle->bpm;
&#125;
unsigned getMusicInitBPM&#40;&#41; &#123;
	return musichandle->inittempo;
&#125;

unsigned setMusicPosition&#40;unsigned arg&#41; &#123;
	unsigned ret = musichandle->sngpos;
	arg = &#40;arg>=musichandle->numpos&#41; ? musichandle->numpos-1 &#58; arg;
	Player_SetPosition&#40;arg&#41;;
	return ret;
&#125;
unsigned getMusicPosition&#40;&#41; &#123;
	return musichandle->sngpos;
&#125;
unsigned getMusicMaxPosition&#40;&#41; &#123;
	return musichandle->numpos-1;
&#125;

void nextMusicPosition&#40;&#41; &#123;
	Player_NextPosition&#40;&#41;;
&#125;
void prevMusicPosition&#40;&#41; &#123;
	Player_PrevPosition&#40;&#41;;
&#125;
luasound.c:

Code: Select all

static int Music_bpm&#40;lua_State *L&#41;
&#123;
	int argc = lua_gettop&#40;L&#41;;
	if&#40;argc != 1 && argc != 0&#41; return luaL_error&#40;L, "wrong number of arguments"&#41;;
	if&#40;argc&#41;
		lua_pushnumber&#40;L, setMusicBPM&#40;luaL_checknumber&#40;L, 1&#41;&#41;&#41;;
	else
		lua_pushnumber&#40;L, getMusicBPM&#40;&#41;&#41;;

	return 1;
&#125;
static int Music_initialBpm&#40;lua_State *L&#41;
&#123;
	int argc = lua_gettop&#40;L&#41;;
	if&#40;argc != 0&#41; return luaL_error&#40;L, "wrong number of arguments"&#41;;
	lua_pushnumber&#40;L, getMusicInitBPM&#40;&#41;&#41;;

	return 1;
&#125;

static int Music_position&#40;lua_State *L&#41;
&#123;
	int argc = lua_gettop&#40;L&#41;;
	if&#40;argc != 1 && argc != 0&#41; return luaL_error&#40;L, "wrong number of arguments"&#41;;
	if&#40;argc&#41;
		lua_pushnumber&#40;L, setMusicPosition&#40;luaL_checknumber&#40;L, 1&#41;&#41;&#41;;
	else
		lua_pushnumber&#40;L, getMusicPosition&#40;&#41;&#41;;

	return 1;
&#125;
static int Music_maxPosition&#40;lua_State *L&#41;
&#123;
	int argc = lua_gettop&#40;L&#41;;
	if&#40;argc != 0&#41; return luaL_error&#40;L, "wrong number of arguments"&#41;;
	lua_pushnumber&#40;L, getMusicMaxPosition&#40;&#41;&#41;;

	return 1;
&#125;

static int Music_nextPosition&#40;lua_State *L&#41;
&#123;
	int argc = lua_gettop&#40;L&#41;;
	if&#40;argc != 0&#41; return luaL_error&#40;L, "wrong number of arguments"&#41;;
	nextMusicPosition&#40;&#41;;

	return 0;
&#125;
static int Music_prevPosition&#40;lua_State *L&#41;
&#123;
	int argc = lua_gettop&#40;L&#41;;
	if&#40;argc != 0&#41; return luaL_error&#40;L, "wrong number of arguments"&#41;;
	prevMusicPosition&#40;&#41;;

	return 0;
&#125;



static const luaL_reg Music_functions&#91;&#93; = &#123;
  &#123;"playFile",          Music_loadAndPlay&#125;,
  &#123;"stop",           	Music_StopAndUnload&#125;,
  &#123;"pause",		 		Music_pause&#125;,
  &#123;"resume",	 		Music_resume&#125;,
  &#123;"playing", 			Music_playing&#125;,
  &#123;"volume", 			Music_volume&#125;,
  &#123;"bpm",			Music_bpm&#125;,
  &#123;"initialBpm",		Music_initialBpm&#125;,
  &#123;"position",			Music_position&#125;,
  &#123;"maxPosition",		Music_maxPosition&#125;,
  &#123;"nextPosition",		Music_nextPosition&#125;,
  &#123;"prevPosition",		Music_prevPosition&#125;,
  &#123;0, 0&#125;
&#125;;
Post Reply