Kleines codeschnipsel wie ich mp3s abspielen konnte (das programm hängt in der endlosschleife da's mir von anfang an nur drauf ankam wie ich richtig dekodierte musik nach /dev/dsp schreiben konnte)

#include <stdio.h>
#include <libmpeg3.h>
#include <sys/soundcard.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>


int main(int argc, char **argv){
        mpeg3_t *file;
        double time;
        short *buffer2;
        short *buffer3;
        short *buffer4;
        unsigned char output_rows[340][240];
        int streams,i,audio_fd;
        int depth;
        int r;
        int retval=0;
        FILE *dsp;
        XSetWindowAttributes attr;
        GC vo_gc;
        Display *mDisplay;
        Window vo_window;
        Window mRootWin;
        int screen;
        screen=DefaultScreen(mDisplay);
        XSetWindowAttributes window_attributes;
        unsigned long window_mask;

        mDisplay=XOpenDisplay((char*)0);
        window_attributes.border_pixel = BlackPixel (mDisplay, screen);
        window_attributes.background_pixel = BlackPixel (mDisplay, screen);
        window_attributes.override_redirect = 0;
        window_mask = CWBackPixel | CWBorderPixel;

        depth = DefaultDepth (mDisplay,DefaultScreen(mDisplay));
        vo_window=XCreateWindow(mDisplay,DefaultRootWindow(mDisplay), 0, 0, 320,240, 0, depth,
                        InputOutput, CopyFromParent,window_mask ,&window_attributes);
        vo_gc=XCreateGC(mDisplay,vo_window,0,0);
        XMapWindow(mDisplay,vo_window);
        XFlush(mDisplay);
//      vo_gc=XCreateGC(mDisplay, vo_window, 0L, &xgcv);
        audio_fd=open("/dev/dsp",O_WRONLY);
//        memset(buffer2,0x0,10240*sizeof(float));
        printf("\n------------------\n");
        file=mpeg3_open("test.mp3"); ///home/revenger/testing/kream204.mpg"); //test.mp3");
        r=16;
        ioctl(audio_fd,SNDCTL_DSP_SETFMT, &r);
        r=16;
        ioctl(audio_fd,SNDCTL_DSP_SETFMT, &r);
        printf("File has audio:  %s\n",mpeg3_has_audio(file)?"Yes":"No");
        printf("File has video:  %s\n",mpeg3_has_video(file)?"Yes":"No");
        streams=1; //1; //mpeg3_total_astreams(file);
        printf("Streams: %d\n",streams);
        streams=1;
        ioctl(audio_fd,SNDCTL_DSP_STEREO,&streams);
        printf("Sample rate: %d\n",mpeg3_sample_rate(file,streams-1));
        r=mpeg3_sample_rate(file,streams-1); //22050; //44100; //44100; //mpeg3_audio_samples(file,streams-1);
        buffer2=(short*)malloc(44100*2);
        buffer3=(short*)malloc(44100*2);
        buffer4=(short*)malloc(88200*2*2);
        retval=ioctl(audio_fd,SNDCTL_DSP_SPEED,&r);
        if(retval==-1){
                printf("couldn't set speed!\n");
        }
        printf("Length: %d\n",mpeg3_audio_samples(file,streams-1));
        while(1){
//              mpeg3_read_frame(file,output_rows,0,0,320,240,320,240,0,1);
                mpeg3_read_audio(file,NULL,buffer2,0,44100,streams-1);
                mpeg3_reread_audio(file,NULL,buffer3,1,44100,streams-1);
                printf("%ld",mpeg3_tell_byte(file));
                for(i=0;i<88200*2;i++){
                        if(i%2==0){
                                buffer4[i]=buffer2[i/2];
                        } else {
                                buffer4[i]=buffer3[i/2];
                        }
                }
                write(audio_fd,buffer4,88200*2);
                printf("file takes: %f seconds\n",mpeg3_get_time(file));
        }
        fclose(dsp);
        mpeg3_close(file);
        return(0);
}

viele CFLAGS wurden bereits von libmpeg3 vorgegeben Makefile

GCC=gcc
CFLAGS=-O3 -march=i686 -fmessage-length=0 -funroll-all-loops -fomit-frame-pointer -falign-loops=2 -falign-jumps=2 -falign-functions=2 -g
CLIBS=-lmpeg3 -L/usr/X11R6/lib/ -lX11

all:
        $(GCC) main.c -o main $(CFLAGS) $(CLIBS)

MarkusRechberger/libmpeg3 (zuletzt geändert am 2007-12-23 22:47:26 durch localhost)