Libmad and Python, help! :)

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
sakya
Posts: 190
Joined: Fri Apr 28, 2006 5:48 pm
Contact:

Libmad and Python, help! :)

Post by sakya »

Hi!

I've never coded in C/C++ before, but I'm trying to add mp3 support to psp-python using libmad. :)
Reading this doumentation: http://docs.python.org/ext/ext.html
and this tutorial http://www.psp-programming.com/tutorials/c/lesson06.htm I've made a module that imports corretcly into a python script, but no sound comes from my psp.
The tutorial's mp3 player example works fine.
Can someone help me? What's wrong?
Many thanks for your attention.

Here's my code:

Code: Select all

#include <Python.h>
#include <pspaudio.h>
#include <pspaudiolib.h>
#include "mp3player.h"

static PyObject *pspmp3Error;

// Init&#58;
static PyObject *
pspmp3_init&#40;PyObject *self, PyObject *args&#41;
&#123;
	int channel;
	if &#40;!PyArg_ParseTuple&#40;args, "i", &channel&#41;&#41;
		return NULL;

	pspAudioInit&#40;&#41;;
	MP3_Init&#40;channel&#41;;
	return Py_None;
&#125;


// Load&#58;
static PyObject *
pspmp3_load&#40;PyObject *self, PyObject *args&#41;
&#123;
	char* fileName;
	if &#40;!PyArg_ParseTuple&#40;args, "s", &fileName&#41;&#41;
		return NULL;

	FILE *fp;
	fp = fopen&#40;fileName, "r"&#41;;
	if &#40;fp&#41;
    &#123;
		MP3_Load&#40;fileName&#41;;
    &#125;
    else
    &#123;
       return NULL;
    &#125;
	return Py_None;
&#125;

// Play&#58;
static PyObject *
pspmp3_play&#40;PyObject *self, PyObject *args&#41;
&#123;
	MP3_Play&#40;&#41;;
	return Py_None;
&#125;


// Stop&#58;
static PyObject *
pspmp3_stop&#40;PyObject *self, PyObject *args&#41;
&#123;
	MP3_Stop&#40;&#41;;
	return Py_None;
&#125;

// Test&#58;
static PyObject *
pspmp3_test&#40;PyObject *self, PyObject *args&#41;
&#123;
	pspAudioInit&#40;&#41;;
	MP3_Init&#40;1&#41;;
	MP3_Load&#40;"test.mp3"&#41;;
	MP3_Play&#40;&#41;;
	while&#40;1&#41;
	&#123;
		if &#40;MP3_EndOfStream&#40;&#41; == 1&#41;
		&#123;
			MP3_Stop&#40;&#41;;
			MP3_FreeTune&#40;&#41;;
			return Py_None;
		&#125;
	&#125;
&#125;


// Definizione metodi modulo&#58;
static PyMethodDef pspmp3Methods&#91;&#93; = &#123;
	&#123;"init", pspmp3_init, METH_VARARGS, "Initialize"&#125;,
	&#123;"load", pspmp3_load, METH_VARARGS, "Load an mp3 file"&#125;,
	&#123;"play", pspmp3_play, METH_VARARGS, "Play the loaded mp3"&#125;,
	&#123;"stop", pspmp3_stop, METH_VARARGS, "Stop the mp3 playback"&#125;,
	&#123;"test", pspmp3_test, METH_VARARGS, ""&#125;,
	&#123;NULL, NULL, 0, NULL&#125;
&#125;;	

// Inizializzazione modulo&#58;
PyMODINIT_FUNC initpspmp3&#40;void&#41;
&#123;
	PyObject *mdl;

	pspmp3Error = PyErr_NewException&#40;"pspmp3.Error", NULL, NULL&#41;;
    mdl = Py_InitModule3&#40;"pspmp3", pspmp3Methods, "PSP Mp3"&#41;;
	if &#40;!mdl&#41;
		return;	
&#125;
If I execute this script the psp dosen't play the mp3:

Code: Select all

import psp2d
import pspmp3
from time import time
from pspos import freemem, getclock, getbus, battery 

msg = ""

#Test mp3&#58;
pspmp3.init&#40;1&#41;
pspmp3.load&#40;"test.mp3"&#41;
pspmp3.play&#40;&#41;

#Main&#58;
screen = psp2d.Screen&#40;&#41;
font = psp2d.Font&#40;'font.png'&#41;
background_image = psp2d.Image&#40;480,272&#41;
background_image.clear&#40;psp2d.Color&#40;0,0,0&#41;&#41;
oldPad = None
number = 0

while True&#58;
  number += 1
  screen.blit&#40;background_image, 0, 0, background_image.width, background_image.height, 0, 0&#41;
  font.drawText&#40;screen, 20, 140, "TIME&#58; " + str&#40;time&#40;&#41;&#41;&#41;
  font.drawText&#40;screen, 20, 160, "MESSAGE&#58; " + msg&#41;

  #Image&#58;
  outerImage = psp2d.Image&#40;100, 100&#41;
  outerImage.clear&#40;psp2d.Color&#40;61, 58, 61, 178&#41;&#41;

  innerImage = psp2d.Image&#40;80, 80&#41;
  innerImage.clear&#40;psp2d.Color&#40;119, 112, 113, 178&#41;&#41;

  msg = "Created " + str&#40;number&#41; + " times"
  screen.blit&#40;outerImage, 0, 0, outerImage.width, outerImage.height, 30, 30&#41;
  screen.blit&#40;innerImage, 0, 0, innerImage.width, innerImage.height, 40, 40&#41;

  newText = "MEM&#58; " + str&#40;freemem&#40;&#41; / 1024&#41; + "kb  CPU&#58;" + str&#40;getclock&#40;&#41;&#41; + "Mhz  BUS&#58;" + str&#40;getbus&#40;&#41;&#41; + "Mhz  BATT&#58;" + str&#40;battery&#40;&#41;&#91;3&#93;&#41; + "%"
  font.drawText&#40;screen, 0, 0, newText&#41;
  screen.swap&#40;&#41;
If I execute the test function the psp hangs, but still no sound output:

Code: Select all

import psp2d
import pspmp3
from time import time
from pspos import freemem, getclock, getbus, battery 

msg = ""

#Test mp3&#58;
pspmp3.test&#40;&#41;

#Main&#58;
screen = psp2d.Screen&#40;&#41;
font = psp2d.Font&#40;'font.png'&#41;
background_image = psp2d.Image&#40;480,272&#41;
background_image.clear&#40;psp2d.Color&#40;0,0,0&#41;&#41;
oldPad = None
number = 0

while True&#58;
  number += 1
  screen.blit&#40;background_image, 0, 0, background_image.width, background_image.height, 0, 0&#41;
  font.drawText&#40;screen, 20, 140, "TIME&#58; " + str&#40;time&#40;&#41;&#41;&#41;
  font.drawText&#40;screen, 20, 160, "MESSAGE&#58; " + msg&#41;

  #Image&#58;
  outerImage = psp2d.Image&#40;100, 100&#41;
  outerImage.clear&#40;psp2d.Color&#40;61, 58, 61, 178&#41;&#41;

  innerImage = psp2d.Image&#40;80, 80&#41;
  innerImage.clear&#40;psp2d.Color&#40;119, 112, 113, 178&#41;&#41;

  msg = "Created " + str&#40;number&#41; + " times"
  screen.blit&#40;outerImage, 0, 0, outerImage.width, outerImage.height, 30, 30&#41;
  screen.blit&#40;innerImage, 0, 0, innerImage.width, innerImage.height, 40, 40&#41;

  newText = "MEM&#58; " + str&#40;freemem&#40;&#41; / 1024&#41; + "kb  CPU&#58;" + str&#40;getclock&#40;&#41;&#41; + "Mhz  BUS&#58;" + str&#40;getbus&#40;&#41;&#41; + "Mhz  BATT&#58;" + str&#40;battery&#40;&#41;&#91;3&#93;&#41; + "%"
  font.drawText&#40;screen, 0, 0, newText&#41;
  screen.swap&#40;&#41;
Post Reply