📄 pocketmp3dlg.cpp
字号:
// PocketMP3Dlg.cpp : implementation file
//
#include "stdafx.h"
#ifdef __WIN32__
#include "mpw_msg.h"
#endif // __WIN32__
#ifndef GUI
#include <iostream.h> // for reporting errors
#endif // GUI
#include "all.h"
#include "crc.h"
#include "header.h"
#include "subband.h"
#include "sublay1.h"
#include "sublay2.h"
#include "synfilt.h"
#include "ibitstr.h"
#include "obuffer.h"
#include "args.h"
#include "layer3.h"
#ifdef SEEK_STOP
#include "mutx_imp.h"
#endif
#include "PocketMP3.h"
#include "PocketMP3Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///////////////////////////////////////////////////
//全局变量
// Argument containers
MPEG_Args _maplay_args;
MPEG_Args *maplay_args = &_maplay_args;
Args *args;
bool MPEG;
Crc16 *crc;
int line_size;
unsigned int scroll_range = 0;
/*
// Copies of header and stream
Header header_copy;
Ibitstream stream_copy;
*/
// WaveOut handle and its address
HWAVEOUT hwo;
HWAVEOUT *phwo = &hwo;
bool use_own_scalefac = false;
float scalefactor=32768.0f;
/////////////////////////////////////////////////////////////////////////////
// CPocketMP3Dlg dialog
CPocketMP3Dlg::CPocketMP3Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CPocketMP3Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPocketMP3Dlg)
m_strName = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
exitMPEG_Thread=CreateEvent(NULL,FALSE,FALSE,NULL);
}
void CPocketMP3Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPocketMP3Dlg)
DDX_Control(pDX, IDC_EDITNAME, m_ctlEditName);
DDX_Control(pDX, IDC_STATUS, m_ctlStatus);
DDX_Control(pDX, IDC_SLIDER, m_ctlSlider);
DDX_Control(pDX, IDC_LIST, m_ctlList);
DDX_Control(pDX, IDC_BTNSTOP, m_ctlBtnStop);
DDX_Control(pDX, IDC_BTNPLAY, m_ctlBtnPlay);
DDX_Control(pDX, IDC_BTNPAUSE, m_ctlBtnPause);
DDX_Control(pDX, IDC_BTNOPEN, m_ctlBtnOpen);
DDX_Text(pDX, IDC_EDITNAME, m_strName);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPocketMP3Dlg, CDialog)
//{{AFX_MSG_MAP(CPocketMP3Dlg)
ON_BN_CLICKED(IDC_BTNPLAY, OnBtnplay)
ON_BN_CLICKED(IDC_BTNOPEN, OnBtnopen)
ON_BN_CLICKED(IDC_BTNPAUSE, OnBtnpause)
ON_BN_CLICKED(IDC_BTNSTOP, OnBtnstop)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPocketMP3Dlg message handlers
BOOL CPocketMP3Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
//设置列表视图控件
CRect rect;
LV_COLUMN lvcolumn;
TCHAR rgtsz[2][20];
m_ctlList.GetWindowRect(&rect);//copies the dimensions of the bounding rectangle of the CWnd object into the structure pointed to by &rect
ScreenToClient(&rect);//This method converts the screen coordinates of a specified point or rectangle on the display to client coordinates.
//设置list对话框的列
_tcscpy(rgtsz[0], _T("歌曲"));
_tcscpy(rgtsz[1], _T("播放所需时间"));
for (int i = 0; i < 2; i++)
{
lvcolumn.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
lvcolumn.fmt = LVCFMT_LEFT;
lvcolumn.pszText = rgtsz[i];
lvcolumn.iSubItem = i;
lvcolumn.cx = rect.Width() *1/ 2;
m_ctlList.InsertColumn(i, &lvcolumn); //插入列
}
filter1 = NULL;
filter2 = NULL;
buffer = NULL;
l3decoder = NULL;
playing=false;
paused=false;
m_ctlStatus.SetWindowText(_T(""));
scroll_range=0;
m_ctlSlider.SetPos(0);
m_ctlEditName.EnableWindow(TRUE);
m_ctlBtnOpen.EnableWindow(TRUE);
m_ctlBtnPlay.EnableWindow(TRUE);
m_ctlBtnPause.EnableWindow(FALSE);
m_ctlBtnStop.EnableWindow(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
bool CPocketMP3Dlg::reinit_MPEG()
{
MPEG = TRUE;
args = maplay_args;
args->hWnd = AfxGetMainWnd()->GetSafeHwnd();
args->mutex = CreateMutex(NULL, FALSE, _T("m"));
args->stop = false;
args->done = false;
args->position_change = false;
args->desired_position = 0;
maplay_args->stream = new Ibitstream((LPCTSTR)m_strName);
maplay_args->MPEGheader = new Header;
maplay_args->phwo = phwo;
maplay_args->which_c = both;
maplay_args->stdout_mode = false;
maplay_args->use_own_scalefactor =use_own_scalefac;
maplay_args->scalefactor = scalefactor;
/*
if (save_mode) {
maplay_args->output_mode = O_WAVEFILE;
} else {
maplay_args->output_mode = output_mode;
}
*/
maplay_args->output_mode = O_WAVEMAPPER;
/*
if (save_mode) {
lstrcpy(maplay_args->output_filename, SaveAsFilename);
} else if (output_mode == O_WAVEFILE) {
lstrcpy(maplay_args->output_filename, OutputFilename);
}
*/
strcpy(maplay_args->output_filename, "");//初始化输出文件名
if (!maplay_args->MPEGheader->read_header(maplay_args->stream, &crc)){
MessageBox(_T("No header found!"),
_T("File format error"), MB_OK | MB_ICONSTOP);
return FALSE;
}
/*
header_copy = *(maplay_args->MPEGheader);
stream_copy = *(maplay_args->stream);
*/
scroll_range = maplay_args->MPEGheader->min_number_of_frames(maplay_args->stream);
line_size = scroll_range >> 3;
//EnableMenuItem(mainmenu, CM_MPEGPROPERTIES, MF_ENABLED);
return TRUE;
}
void CPocketMP3Dlg::maplay_Exit(uint32 returncode)
{
#ifdef SEEK_STOP
if ((returncode == 1) && buffer) {
buffer->set_stop_flag();
}
#endif // SEEK_STOP
if(NULL!=NULL)
{
delete buffer;//这句代码耗时比较长
buffer = NULL;
}
if(filter1!=NULL)
{
delete filter1;
filter1 = NULL;
}
if(filter2!=NULL)
{
delete filter2;
filter2 = NULL;
}
if(l3decoder!=NULL)
{
delete l3decoder;
l3decoder = NULL;
}
playing=false;
m_ctlEditName.EnableWindow(TRUE);
m_ctlBtnOpen.EnableWindow(TRUE);
m_ctlBtnPlay.EnableWindow(TRUE);
m_ctlBtnPause.EnableWindow(FALSE);
m_ctlBtnStop.EnableWindow(FALSE);
m_ctlSlider.SetPos(0);
}
uint32 CPocketMP3Dlg::maplay(LPVOID pVoid)
{
CPocketMP3Dlg *pThis=(CPocketMP3Dlg *)pVoid;
uint32 layer;
enum e_mode mode;
bool read_ready = false, write_ready = false;
// These arguments should not change while decoding
Crc16 *crc = NULL;
Ibitstream *stream = maplay_args->stream;
Header *header = maplay_args->MPEGheader;
enum e_channels which_channels = maplay_args->which_c;
#ifdef SEEK_STOP
_Mutex mutex = maplay_args->mutex;
#endif
#ifdef WIN32GUI
HWND hWnd = maplay_args->hWnd;
#else
// copy your operating system dependent arguments here
#endif // WIN32GUI
// get info from header of first frame:
layer = header->layer();
if ((mode = header->mode()) == single_channel)
which_channels = left;
// create filter(s):
{
real scalefactor = (maplay_args->use_own_scalefactor) ?
maplay_args->scalefactor :
32768.0f;
pThis->filter1 = new SynthesisFilter(0, scalefactor);
if ((mode != single_channel) && (which_channels == both))
pThis->filter2 = new SynthesisFilter(1, scalefactor);
}
// create buffer, and check to see if created ok:
#ifdef __WIN32__
/* if (maplay_args->stdout_mode) {
buffer = create_stdout_obuffer(maplay_args);
} else {*/
switch (maplay_args->output_mode) {
case O_DIRECTSOUND:
// buffer = create_ds_obuffer(maplay_args);
break;
case O_WAVEMAPPER:
pThis->buffer = create_obuffer(maplay_args);
break;
case O_WAVEFILE:
//pThis->buffer = create_Wavefile_obuffer(maplay_args);
break;
// }
}
#else
/* buffer = (maplay_args->stdout_mode) ?
create_stdout_obuffer(maplay_args) :
create_obuffer(maplay_args);*/
buffer=create_obuffer(maplay_args);
#endif
if (pThis->buffer == NULL) {
pThis->maplay_Exit(0);
return 1;
}
// Layer III : initialize decoder
if (layer == 3)
pThis->l3decoder = new LayerIII_Decoder(stream, header, pThis->filter1,
pThis->filter2, pThis->buffer, which_channels);
do
{
if(WaitForSingleObject(pThis->exitMPEG_Thread,0)==WAIT_OBJECT_0)
{
TerminateThread(pThis->MPEG_Thread, 0);
waveOutReset(*phwo);
waveOutClose(*phwo);
goto PreReturnCleanup;
}
#ifdef SEEK_STOP
mtx_lock(mutex);
if (maplay_args->stop)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -