您尚未登录。

#1 2012-08-21 18:04:54

jouyouyun
会员
注册时间: 2011-10-10
帖子: 268

线程问题,哪位大神帮忙看看?(已解决)

下面这段函数通过使用 libmad 库来播放 MP3, 程序可以运行。现在想做图形界面(QT),想用线程来实现播放和暂停,但一直都没能成功。求解答,到底该在哪里创建线程?
具体代码如下:

/*
 * libmad - MPEG audio decoder library
 * Copyright (C) 2000-2004 Underbit Technologies, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: minimad.c,v 1.4 2004/01/23 09:41:32 rob Exp $
 */

# include <stdio.h>
# include <unistd.h>
# include <sys/stat.h>
# include <sys/types.h>
# include <sys/mman.h>
# include <fcntl.h>
# include <stdlib.h>
# include <string.h>

# include <linux/soundcard.h>

# include "mad.h"

/*
 * This is perhaps the simplest example use of the MAD high-level API.
 * Standard input is mapped into memory via mmap(), 
 * then the high-level API
 * is invoked with three callbacks: input, output, and error. The output
 * callback converts MAD's high-resolution PCM samples to 16 bits, then
 * writes them to standard output in little-endian, stereo-interleaved
 * format.
 * 
 * 以帧的形式写入
 */


#define BUFSIZE (8*1024)

struct buffer {
	FILE 	*fp;
	unsigned int flen;
	unsigned int fpos;
	unsigned char fbuf[BUFSIZE];
	unsigned int fbsize;
};

//static int decode(unsigned char const *, unsigned long);
static int decode(struct buffer*);

int sounded;
unsigned int prerate = 0;

int write_dsp(int c)
{
	return write(sounded,(char*)&c,1);
}

void set_dsp()
{
	int format = AFMT_S16_LE;
	int channels = 2;
	sounded = open("/dev/dsp",O_WRONLY);
	ioctl(sounded,SNDCTL_DSP_SETFMT,&format);
	ioctl(sounded,SNDCTL_DSP_CHANNELS,&channels);

}

int main(int argc, char *argv[])
{
	long flen, fsta, fend;
	int dlen;
	struct buffer* mp3fp;

	if (argc != 2)
		return 1;
	
	mp3fp = (struct buffer*)malloc(sizeof(struct buffer));

	
	mp3fp->fp = fopen(argv[1],"r+");
	if(!mp3fp->fp)
	{
		printf("Can't open source file .\n");
		return 2;
	}
	fsta = ftell(mp3fp->fp);
	fseek(mp3fp->fp,0,SEEK_END);
	fend = ftell(mp3fp->fp);
	flen = fend - fsta;
	
	fseek(mp3fp->fp,0,SEEK_SET);
	fread(mp3fp->fbuf,1,BUFSIZE,mp3fp->fp);
	mp3fp->fbsize 	= BUFSIZE;
	mp3fp->fpos 	= BUFSIZE;
	mp3fp->flen	= flen;
	set_dsp();
	decode(mp3fp);
	close(sounded);
	fclose(mp3fp->fp);
	return 0;
}

/*
 * This is a private message structure. A generic pointer to this structure
 * is passed to each of the callback functions. Put here any data you need
 * to access from within the callbacks.
 */

/*
 * This is the input callback. The purpose of this callback is to (re)fill
 * the stream buffer which is to be decoded. In this example, an entire file
 * has been mapped into memory, so we just call mad_stream_buffer() with the
 * address and length of the mapping. When this callback is called a second
 * time, we are finished decoding.
 */

static enum mad_flow input(void *data,
		struct mad_stream *stream)
{
	struct buffer *buffer = data;
	int	ret_code;
	int 	unproc_data_size;	//未解压的数据
	int	copy_size;

	if (buffer->fpos <= buffer->flen)
	{
		//printf("input...\n");
		unproc_data_size = stream->bufend - stream->next_frame;
		//fbsize = BUFSIZE,表示fbuf的长度
		memcpy(buffer->fbuf,buffer->fbuf+buffer->fbsize-unproc_data_size,
			unproc_data_size);
		copy_size = BUFSIZE - unproc_data_size;
		if(buffer->fpos + copy_size > buffer->flen)
		{
			copy_size = buffer->flen - buffer->fpos;
		}
		fread(buffer->fbuf + unproc_data_size,1,copy_size,buffer->fp);
		buffer->fbsize = unproc_data_size + copy_size;
		buffer->fpos += copy_size;
		mad_stream_buffer(stream,buffer->fbuf,buffer->fbsize);
		ret_code = MAD_FLOW_CONTINUE;
	}	
	else
	{
		printf("MAD_FLOW_STOP\n");
		ret_code = MAD_FLOW_STOP;
	}

	return ret_code;
}

/*
 * The following utility routine performs simple rounding, clipping, and
 * scaling of MAD's high-resolution samples down to 16 bits. It does not
 * perform any dithering or noise shaping, which would be recommended to
 * obtain any exceptional audio quality. It is therefore not recommended to
 * use this routine if high-quality output is desired.
 */

static inline signed int scale(mad_fixed_t sample)
{
	/* round */
	sample += (1L << (MAD_F_FRACBITS - 16));

	/* clip */
	if (sample >= MAD_F_ONE)
		sample = MAD_F_ONE - 1;
	else if (sample < -MAD_F_ONE)
		sample = -MAD_F_ONE;

	/* quantize */
	return sample >> (MAD_F_FRACBITS + 1 - 16);
}

/*
 * This is the output callback function. It is called after each frame of
 * MPEG audio data has been completely decoded. The purpose of this callback
 * is to output (or play) the decoded PCM audio.
 */

static enum mad_flow output(void *data,
		struct mad_header const *header,
		struct mad_pcm *pcm)
{
	unsigned int nchannels, nsamples;
	unsigned int rate;
	mad_fixed_t const *left_ch, *right_ch;

	/* pcm->samplerate contains the sampling frequency */

	rate = pcm->samplerate;
	nchannels = pcm->channels;
	nsamples  = pcm->length;
	left_ch   = pcm->samples[0];
	right_ch  = pcm->samples[1];

	if(rate != prerate)
	{
		ioctl(sounded,SNDCTL_DSP_SPEED,&rate);
		prerate = rate;
	}

	while (nsamples--) {
		signed int sample;

		/* output sample(s) in 16-bit signed little-endian PCM */

		sample = scale(*left_ch++);
		write_dsp((sample >> 0) & 0xff);
		write_dsp((sample >> 8) & 0xff);

		if (nchannels == 2) {
			sample = scale(*right_ch++);
			write_dsp((sample >> 0) & 0xff);
			write_dsp((sample >> 8) & 0xff);
		}
	}

	return MAD_FLOW_CONTINUE;
}

/*
 * This is the error callback function. It is called whenever a decoding
 * error occurs. The error is indicated by stream->error; the list of
 * possible MAD_ERROR_* errors can be found in the mad.h (or stream.h)
 * header file.
 */

static enum mad_flow error(void *data,
		struct mad_stream *stream,
		struct mad_frame *frame)
{
	struct buffer *buffer = data;

	fprintf(stderr, "decoding error 0x%04x (%s) at byte offset %u\n",
			stream->error, mad_stream_errorstr(stream),
			stream->this_frame - buffer->fbuf);

	/* return MAD_FLOW_BREAK here to stop decoding (and propagate an error) */

	return MAD_FLOW_CONTINUE;
}

/*
 * This is the function called by main() above to perform all the decoding.
 * It instantiates a decoder object and configures it with the input,
 * output, and error callback functions above. A single call to
 * mad_decoder_run() continues until a callback function returns
 * MAD_FLOW_STOP (to stop decoding) or MAD_FLOW_BREAK (to stop decoding and
 * signal an error).
 */
	
//int decode(unsigned char const *start, unsigned long length)
static int decode(struct buffer* mp3fp)
{
	struct mad_decoder decoder;
	int result;

	/* configure input, output, and error functions */

	mad_decoder_init(&decoder, mp3fp,
			input, 0 /* header */, 0 /* filter */, output,
			error, 0 /* message */);

	/* start decoding */

	result = mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);

	/* release the decoder */

	mad_decoder_finish(&decoder);

	return result;
}

最近编辑记录 jouyouyun (2012-08-22 18:18:36)

离线

#2 2012-08-21 20:55:43

jouyouyun
会员
注册时间: 2011-10-10
帖子: 268

Re: 线程问题,哪位大神帮忙看看?(已解决)

怎么没人回?是我说的不够清楚?在界面中有播放、暂停等按钮,如果用上面的程序,点击播放后,程序开始放歌,界面上的其他按钮就无法使用。除了歌放完,或者强行终止,无法退出。现在想用线程来控制,好达到播放的时候其他的按钮可以使用,不至于使程序被独占。

离线

#3 2012-08-22 09:21:01

依云
会员
所在地: a.k.a. 百合仙子
注册时间: 2011-08-21
帖子: 8,934
个人网站

Re: 线程问题,哪位大神帮忙看看?(已解决)

不会 qt 也不会放歌。更不知道你遇到的问题到底是什么。开个 qt 的线程去放歌呗,有什么问题么?

离线

#4 2012-08-22 15:27:48

jouyouyun
会员
注册时间: 2011-10-10
帖子: 268

Re: 线程问题,哪位大神帮忙看看?(已解决)

我是用QT做界面,用上面的代码来播放。界面中的 play 按钮的动作连接到这里面的 main(按照上面的代码,QT中会改名) 函数,调用 decode 函数来播放。  input 函数解码, ouput 函数把解出的码写入到 /dev/dsp 设备中,以此来播放歌曲。 decode 中 mad_decoder_init 函数来回调 input、output、err,根据 input、 output 的返回值来决定是否循环调用。返回值为 MAD_FLOW_CONTINUE 表示循环调用。

离线

#5 2012-08-22 18:18:14

jouyouyun
会员
注册时间: 2011-10-10
帖子: 268

Re: 线程问题,哪位大神帮忙看看?(已解决)

找到了,在 main 中创建线程调用 decode() 函数,把关闭 文件描述符 的操作移到 input 函数里去(放在 else 里面)。

离线

页脚