Ejemplo n.º 1
0
static cxBool cxMp3StreamOpen(cxAny stream)
{
    cxMp3Stream this = stream;
    this->fd = open(cxStringBody(this->path), O_RDONLY, 0600);;
    if(this->fd <= 0){
        CX_ERROR("open strean file %s failed",cxStringBody(this->path));
        return false;
    }
    int error = 0;
    this->mh = mpg123_new(NULL, &error);
    if(this->mh == NULL){
        CX_ERROR("mpg123 new failed");
        return false;
    }
    if(mpg123_open_fd(this->mh, this->fd) != MPG123_OK){
        CX_ERROR("open mpg file failed %s",cxStringBody(this->path));
        return false;
    }
    allocator->free(this->buffer);
    this->bufsiz = mpg123_outblock(this->mh);
    this->buffer = allocator->malloc(this->bufsiz);
    mpg123_getformat(this->mh, &this->freq, &this->channels, &this->encoding);
    this->super.canRead = true;
    this->super.canSeek = true;
    this->super.isOpen = true;
    return true;
}
Ejemplo n.º 2
0
int stream_open(stream *stream, const char *url)
{
    fprintf(stderr, "stream_open: %s\n", url);
    stream->url = sdsnew(url);

    response *response = resolve_stream(url);

    if (response == NULL) {
        return -1;
    }

    if (stream->fd > 3 && fd_is_valid(stream->fd)) {
        close(stream->fd);
        mpg123_close(stream->mpg123);
    }
    
    stream->fd = response->fd;
    free_response(response);

    if (mpg123_open_fd(stream->mpg123, stream->fd) != MPG123_OK) {
        return -1;
    }
    
    return 0;
}
Ejemplo n.º 3
0
static int open_track_fd (void)
{
	/* Let reader handle invalid filept */
	if(mpg123_open_fd(mh, filept) != MPG123_OK)
	{
		error2("Cannot open fd %i: %s", filept, mpg123_strerror(mh));
		return 0;
	}
	debug("Track successfully opened.");
	fresh = TRUE;
	return 1;
	/*1 for success, 0 for failure */
}
Ejemplo n.º 4
0
Archivo: mp3.c Proyecto: XanClic/xply
static bool is_mp3(FILE *fp, struct file_type **ft)
{
    mpg123_init();

    int err;
    mpg123_handle *mh = mpg123_new(NULL, &err);

    if (mh == NULL)
    {
        fprintf(stderr, "Could not create mpg123 handle: %s\n", mpg123_plain_strerror(err));
        return false;
    }

    long pos = ftell(fp);

    if (mpg123_open_fd(mh, fileno(fp)) != MPG123_OK)
    {
        fseek(fp, pos, SEEK_SET);
        return false;
    }

    mpg123_scan(mh);

    struct mpg123_frameinfo fi;
    mpg123_info(mh, &fi);

    if (mpg123_format(mh, fi.rate, MPG123_STEREO, MPG123_ENC_SIGNED_16) != MPG123_OK)
    {
        fseek(fp, pos, SEEK_SET);
        return false;
    }

    off_t length = mpg123_length(mh);

    struct mp3_format *fmt = calloc(sizeof(*fmt), 1);
    *ft = &fmt->ft;
    fmt->mh = mh;
    fmt->tpf = mpg123_tpf(mh);
    fmt->ft.channels = 2;
    fmt->ft.position = 0;
    fmt->ft.length = (length + (fi.rate / 2)) / fi.rate;
    fmt->ft.sample_rate = fi.rate;
    fmt->ft.sample_size = 2;
    fmt->ft.sample_type = ST_SIGNED_INTEGER_LE;
    fmt->ft.bitrate = (double)fi.abr_rate;

    return true;
}
Ejemplo n.º 5
0
int mp3Init(struct playerHandles *ph){
	pthread_mutex_init(&dechandle_lock,NULL);

	mpg123_init();

	h.m = mpg123_new(NULL, NULL);
	if(h.m == NULL){
		fprintf(stderr,"Unable to create mpg123 handle\n");
		return -1;
	}
	//mpg123_param(h.m, MPG123_VERBOSE, 0, 0);
	mpg123_param(h.m, MPG123_RESYNC_LIMIT, -1, 0);
	mpg123_param(h.m, MPG123_FLAGS, MPG123_QUIET, 0);
	//mpg123_open_feed(h.m);
	mpg123_open_fd(h.m,fileno(ph->ffd));
	if(h.m == NULL) return -1;

	return 0;
}
Ejemplo n.º 6
0
bool Mp3Decoder::openFile( FILE*& file )
{
	_handle = mpg123_new(NULL, NULL);
	if(_handle == NULL)
	{
		printf("mpg123_new failed\n");
		return false;
	}

	_file = file;

	int res = mpg123_open_fd(_handle, _file->_file);
	if(res != MPG123_OK)
	{
		printf("mpg123_open_handle failed\n");
		return false;
	}

	return afterOpen();
}
Ejemplo n.º 7
0
int mp3decode_reg(struct xlplayer *xlplayer)
    {
    static pthread_once_t once_control = PTHREAD_ONCE_INIT;
    struct mp3decode_vars *self;
    struct chapter *chapter;
    int fd, rv;
    long rate;
    int channels, encoding;
    int src_error;

    pthread_once(&once_control, decoder_library_init);
    if (!decoder_library_ok)
        {
        fprintf(stderr, "mp3decode_reg: decoder library is not ok\n");
        goto rej;
        }


    if (!(self = xlplayer->dec_data = calloc(1, sizeof (struct mp3decode_vars))))
        {
        fprintf(stderr, "mp3decode_reg: malloc failure\n");
        goto rej;
        }


    if (!(self->mh = mpg123_new(NULL, NULL)))
        {
        fprintf(stderr, "mp3decode_reg: handle not okay");
        goto rej_;
        }

#ifdef MPG123_AUTO_RESAMPLE
    if (mpg123_param(self->mh, MPG123_REMOVE_FLAGS, MPG123_AUTO_RESAMPLE, 0.0) != MPG123_OK)
        {
        fprintf(stderr, "mpgdecode_reg: failed to turn off auto resampling\n");
        goto rej_;
        }
#endif

    if (mpg123_param(self->mh, MPG123_ADD_FLAGS, MPG123_FORCE_STEREO, 0.0) != MPG123_OK)
        {
        fprintf(stderr, "mpgdecode_reg: failed to set flags");
        goto rej_;
        }

    if (mpg123_format_none(self->mh) != MPG123_OK)
        {
        fprintf(stderr, "mp3decode_reg: failed to clear output formats");
        goto rej_;
        }

    /* all the permitted mp3 sample rates are enabled
     * forced stereo is in effect so no need to add mono formats
     */
    mpg123_format(self->mh, 48000, MPG123_STEREO, MPG123_ENC_FLOAT_32);
    mpg123_format(self->mh, 44100, MPG123_STEREO, MPG123_ENC_FLOAT_32);
    mpg123_format(self->mh, 32000, MPG123_STEREO, MPG123_ENC_FLOAT_32);
    mpg123_format(self->mh, 24000, MPG123_STEREO, MPG123_ENC_FLOAT_32);
    mpg123_format(self->mh, 22050, MPG123_STEREO, MPG123_ENC_FLOAT_32);
    mpg123_format(self->mh, 16000, MPG123_STEREO, MPG123_ENC_FLOAT_32);
    mpg123_format(self->mh, 12000, MPG123_STEREO, MPG123_ENC_FLOAT_32);
    mpg123_format(self->mh, 11025, MPG123_STEREO, MPG123_ENC_FLOAT_32);
    mpg123_format(self->mh, 8000, MPG123_STEREO, MPG123_ENC_FLOAT_32);

    if (!(self->fp = fopen(xlplayer->pathname, "r")))
        {
        fprintf(stderr, "mp3decode_reg: failed to open %s\n", xlplayer->pathname);
        goto rej_;
        }

    mp3_tag_read(&self->taginfo, self->fp);
    lseek(fd = fileno(self->fp), 0, SEEK_SET);

    if ((rv = mpg123_open_fd(self->mh, fd)) != MPG123_OK)
        {
        fprintf(stderr, "mp3decode_reg: mpg123_open_fd failed with return value %d\n", rv);
        goto rej__;
        }
        
    if (mpg123_getformat(self->mh, &rate, &channels, &encoding) != MPG123_OK || channels != 2)
        {
        fprintf(stderr, "mp3decode_reg: mpg123_getformat returned unexpected value\n");
        goto rej___;
        }
    
    if (rate != xlplayer->samplerate)
        {
        fprintf(stderr, "mp3decode_reg: configuring resampler\n");

        xlplayer->src_state = src_new(xlplayer->rsqual, channels, &src_error);
        if (src_error)
            {
            fprintf(stderr, "mp3decode_reg: src_new reports %s\n", src_strerror(src_error));
            goto rej___;
            }

        xlplayer->src_data.src_ratio = (double)xlplayer->samplerate / (double)rate;
        xlplayer->src_data.end_of_input = 0;
        
        size_t output_frames = (size_t)(xlplayer->src_data.src_ratio * 1.1 * 1152);
        xlplayer->src_data.output_frames = (long)output_frames;
        if (!(xlplayer->src_data.data_out = malloc(output_frames * 2 * sizeof (float))))
            {
            fprintf(stderr, "mp3decode_reg: malloc failure\n");
            goto rej____;
            }

        self->resample = TRUE;
        }

    xlplayer->dec_init = mp3decode_init;
    xlplayer->dec_play = mp3decode_play;
    xlplayer->dec_eject = mp3decode_eject;


    if ((chapter = mp3_tag_chapter_scan(&self->taginfo, xlplayer->play_progress_ms + 70)))
        {
        self->current_chapter = chapter;
        xlplayer_set_dynamic_metadata(xlplayer, dynamic_metadata_form[chapter->title.encoding], chapter->artist.text, chapter->title.text, chapter->album.text, 0);
        }

    if (xlplayer->seek_s)
        if (mpg123_seek(self->mh, (off_t)rate * xlplayer->seek_s, SEEK_SET) < 0)
            {
            fprintf(stderr, "mp3decode_init: seek failed\n");
            mp3decode_eject(xlplayer);
            xlplayer->playmode = PM_STOPPED;
            xlplayer->command = CMD_COMPLETE;
            }

    return ACCEPTED;

    rej____:
    xlplayer->src_state = src_delete(xlplayer->src_state);
    rej___:
    mpg123_delete(self->mh);
    rej__:
    mp3_tag_cleanup(&self->taginfo);
    fclose(self->fp);
    rej_:
    free(self);
    rej:
    return REJECTED;
    }
Ejemplo n.º 8
0
/* Open an MPEG stream and prepare for decode */
static int libmpg123_init(const char *fn) {
	int err;
	uint32	fd;
	
	/* Open the file */
#ifdef BS_SIZE
	mp3_fd = fd = fs_open(fn, O_RDONLY);
#else
	fd = fs_open(fn, O_RDONLY);
#endif
	
	if (fd < 0) {
		printf("Can't open input file %s\r\n", fn);
		printf("getwd() returns '%s'\r\n", fs_getwd());
		return -1;
	}
	
#ifndef BS_SIZE	
	fs_close(fd);
#endif
	
	if (fn != mp3_last_fn) {
		if (fn[0] != '/') {
			strcpy(mp3_last_fn, fs_getwd());
			strcat(mp3_last_fn, "/");
			strcat(mp3_last_fn, fn);
		} else {
			strcpy(mp3_last_fn, fn);
		}
	}

	/* Allocate buffers */
	if (pcm_buffer == NULL)
		pcm_buffer = malloc(PCM_SIZE);
	pcm_ptr = pcm_buffer; pcm_count = pcm_discard = 0;

	
#ifdef BS_SIZE

	if (bs_buffer == NULL)
		bs_buffer = malloc(BS_SIZE);
	bs_ptr = bs_buffer; bs_count = 0;

	/* Fill bitstream buffer */
	if (bs_fill() < 0) {
		printf("Can't read file header\r\n");
		goto errorout;
	}

	/* Are we looking at a RIFF file? (stupid Windows encoders) */
	if (bs_ptr[0] == 'R' && bs_ptr[1] == 'I' && bs_ptr[2] == 'F' && bs_ptr[3] == 'F') {
		/* Found a RIFF header, scan through it until we find the data section */
		printf("Skipping stupid RIFF header\r\n");
		while (bs_ptr[0] != 'd' || bs_ptr[1] != 'a' || bs_ptr[2] != 't'	|| bs_ptr[3] != 'a') {
			bs_ptr++;
			if (bs_ptr >= (bs_buffer + BS_SIZE)) {
				printf("Indeterminately long RIFF header\r\n");
				goto errorout;
			}
		}

		/* Skip 'data' and length */
		bs_ptr += 8;
		bs_count -= (bs_ptr - bs_buffer);
		printf("Final index is %d\r\n", (bs_ptr - bs_buffer));
	}

	if (((uint8)bs_ptr[0] != 0xff) && (!((uint8)bs_ptr[1] & 0xe0))) {
		printf("Definitely not an MPEG file\r\n");
		goto errorout;
	}
#endif
	
	mpg123_init();
    mh = mpg123_new(NULL, &err);

	if(mh == NULL) {
		printf("Can't init mpg123: %s\n", mpg123_strerror(mh));
		goto errorout;
	}
   
    /* Open the MP3 context in open_fd mode */
#ifdef BS_SIZE
    err = mpg123_open_fd(mh, mp3_fd);
#else
	err = mpg123_open(mh, fn);
#endif
 
	if(err != MPG123_OK) {
		printf("Can't open mpg123\n");
		mpg123_exit();
		goto errorout;
	}

	int enc;

	mpg123_getformat(mh, &rate, &channels, &enc);
	mpg123_info(mh, &decinfo);
	
	printf("Output Sampling rate = %ld\r\n", decinfo.rate);
	printf("Output Bitrate       = %d\r\n", decinfo.bitrate);
	printf("Output Frame size    = %d\r\n", decinfo.framesize);

	printf("mpg123 initialized successfully\r\n");
	return 0;

errorout:
	printf("Exiting on error\r\n");
#ifdef BS_SIZE
	if (bs_buffer) {
		free(bs_buffer);
		bs_buffer = NULL;
	}
#endif
	if (pcm_buffer) {
		free(pcm_buffer);
		pcm_buffer = NULL;
	}
#ifdef BS_SIZE
	fs_close(fd);
	mp3_fd = 0;
#endif
	return -1;
}
Ejemplo n.º 9
0
static int
stream_loop(mpg123_handle *mh, int socket)
{
    ao_device *dev;
    ao_sample_format format;
    size_t bytes_decoded;
    int status;
    unsigned char out_buffer[OUT_BUF_SIZE];
    int channels;
    int encoding;
    long rate;

    /* listen to socket */
    if (mpg123_open_fd(mh, socket) == MPG123_ERR) {
        fprintf(stderr, "%s\n", mpg123_strerror(mh));
        return 1;
    }

    status = MPG123_OK;

    /* set some standard format */
    memset(&format, 0, sizeof(ao_sample_format));
    format.rate = 44100;
    format.channels = 2;
    format.byte_format = AO_FMT_LITTLE;
    format.bits = 16;
    dev = ao_open_live(ao_default_driver_id(), &format, NULL);
    if (!dev) {
        fprintf(stderr, "ao_open_live failed()");
        return 1;
    }

    do {
        if (g_paused) {
            fprintf(stderr, "paused\n");
            sleep(1);
            continue;
        }
        status = mpg123_read(mh, out_buffer, OUT_BUF_SIZE, &bytes_decoded);
        if (status != 0) {
            fprintf(stderr, "status [%d] - %s\n", status, mpg123_plain_strerror(status));
        }
        if (status == MPG123_ERR || status == MPG123_DONE) {
            break;
        } else { 
            if (status == MPG123_NEW_FORMAT) {
                status = mpg123_getformat(mh, &rate, &channels, &encoding);
                fprintf(stderr, "new format:\n\trate: %ld\n\tchannels:%d\n\tencoding: %d\n",
                        rate, channels, encoding);
                format.rate = rate;
                format.channels = channels;
                format.byte_format = AO_FMT_LITTLE;
                format.bits = mpg123_encsize(encoding) * 8;
                /* close and reopen again for new format */
                ao_close(dev);
                dev = ao_open_live(ao_default_driver_id(), &format, NULL);
                if (!dev) {
                    fprintf(stderr, "ao_open_live failed\n");
                    break;
                }
            }
            if (dev) {
                ao_play(dev, (char*)out_buffer, bytes_decoded);
            }
        }
    } while (g_go_on && !g_stop);

    if (dev) {
        fprintf(stderr, "stderr close ao device\n");
        ao_close(dev);
    }
    return status;
}
Ejemplo n.º 10
0
void getMusInfo( const char *fileE, uint type, int *time, QString *title )
{
	if ( type == 2 )
	{
		if ( title )
			*title = "";
		if ( time )
			*time = -1;
		return;
	}
	if ( type != 0 )
	{
		if ( title )
			*title = "";
		if ( time )
			*time = -2;
		return;
	}
	if ( type == 0 )
	{
		if ( !title || !time )
			return;

		mpg123_handle *mus = NULL;
		mus = mpg123_new(NULL, NULL);
		int fd = qmp_open( fileE, O_RDONLY|O_BINARY );
		mpg123_open_fd( mus, fd );

		if ( !mus )
		{
			mpg123_close(mus);
			mpg123_delete(mus);
			if ( fd > -1 )
				close( fd );

			if ( title )
				*title = "";
			if ( time )
				*time = -2;

			return;
		}

		QString t;

		bool OK(0);
		if ( !notReadID3 )
			OK = getMusInfo( mus, NULL, NULL, &t, NULL, NULL, type, fileE );
		else
			OK = getMusInfo( mus, NULL, NULL, NULL, NULL, NULL, type, fileE );

		if ( !OK )
		{
			mpg123_close(mus);
			mpg123_delete(mus);
			if ( fd > -1 )
				close( fd );

			if ( title )
				*title = "";
			if ( time )
				*time = -2;

			return;
		}

		if ( !notReadID3 && title )
			*title = t;

		if ( time )
		{
			double lSec;
			mpg123_position(mus,0,0,0,0,0,&lSec);
			*time = lSec;
		}

		mpg123_close(mus);
		mpg123_delete(mus);
		if ( fd > -1 )
			close( fd );
	}
}
Ejemplo n.º 11
0
void play( const char *fileE, int )
{
	if ( Type != 0 && Type != 2 )
		return ERROR2( "Wtyczka obsługuje tylko odczyt plików i strumienia internetowego!" );

	if ( Type == 2 )
	{
		QString tmp = loadCURL();
		if ( !CURLloaded )
			return ERROR2( "Nie można załadować biblioteki: "+QMPInternetf+libExt+"\n"+tmp );
	}

	if ( fileE != curF )
		clrPos();

	clrSet();

	QString BLAD;
	if ( Type == 0 )
		BLAD = "Błąd odczytu pliku!";
	else if ( Type == 2 )
		BLAD = "Nie można otworzyć strumienia internetowego!";

	musicfile = mpg123_new(NULL, NULL);
	if ( Type == 0 )
	{
		fd = qmp_open( fileE, O_RDONLY|O_BINARY );
		mpg123_open_fd( musicfile, fd );
	}
	if ( Type == 2 )
	{
		title = "Czekaj, trwa otwieranie...";

		mpg123_open_feed( musicfile );

		f = url_fopen(fileE,0,1);
		if ( !f )
			return ERROR(BLAD);

		if ( !getDataBuff( f, url_fread, _DATA_BUFF, &data, &bolStop ) )
			return ERROR(BLAD);

		url_fread(data+10, 1, _DATA_BUFF-10, f);

		if ( getNetInfo( f, FILE_SIZE ) > 0.0 )
			knownSize = true;
		mpg123_decode( musicfile, (const unsigned char*)data, _DATA_BUFF, 0,0,0 );
	}

	if ( !musicfile )
		return ERROR(BLAD);

	curF = fileE;

	if ( !MP3Update() )
		return ERROR(BLAD);

	if ( Type == 2 && title.isEmpty() )
		title = fileE;

	if ( Type == 2 || ( Type == 0 && fileE[0] == '/' && fileE[1] == '/' ) )
		QOut->useQMPBuffer();
	QOut->Init( AudioInfo( rate, 16, chn ), true, 0, fileE, /*title*/Title );

	if ( *QOut->PlErr )
	{
		*QOut->PlErr = false;
		return ERROR("Błąd zwraca wtyczka wyjściowa!");
	}

	IsPlaying = true;
	size_t bDecoded = 0;
	audio_buffer = new char[BUF_SIZE];
	TMPint = 0;
	int musErr = 0;
	for(;;)
	{
		if ( bolStop )
			break;
		if ( !IsPaused )
		{
			if ( !*QOut->mustReset )
			{
				if ( doSeek )
				{
					mpg123_seek( musicfile, doSeek, SEEK_SET );
					QOut->control( QOUT_NEXT_READ );
					doSeek = 0;
				}

				musErr = mpg123_read( musicfile, (unsigned char*)audio_buffer, BUF_SIZE, &bDecoded );
				if ( musErr == MPG123_DONE )
					break;
				if ( musErr == MPG123_ERR && !searchParts )
					break;
				QOut->Play( audio_buffer, bDecoded, pltime, false );
				MP3Update2( bDecoded );
				if ( musErr == MPG123_NEED_MORE && Type != 0 )
				{
					int bread = url_fread( data, 1, _DATA_BUFF, f );
					if ( ( !bread && !wait4Data ) || ( !bread && url_feof(f) ) )
						break;
					else if ( !bread )
						MYsleep( 25 );
					mpg123_decode( musicfile, (const unsigned char*)data, bread, NULL, 0, NULL );
				}
			}
			else
				QOut->Play(0,0,pltime,true);
		}
		else
			QOut->Play(0,0,pltime,true);
	}
	STOP();
}