📄 ttplaydlg.cpp
字号:
// ttplayDlg.cpp : implementation file
//
#include<iostream.h>
#include<string.h>
#include "memory.h"
#include<afxwin.h>
#include<windows.h>
#include<iomanip.h>
//#include "g72x.h"
#include"g726.h"
#include "stdafx.h"
#include "ttplay.h"
#include "ttplayDlg.h"
#include "stdio.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern "C" void g726_Decode(char *Adpcm,short *pcm);
threadInfo Info;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class RIFF{
public:
char riffID[4]; //RIFF标识
long riffSIZE; //文件长度减8 字节
char riffFORMAT[4]; //WAVE标识:"WAVE"
public:
RIFF(char* ="RIFF",long=0,char* ="WAVE");
};
RIFF::RIFF(char*riffid,long riffsize,char*riffformat){
strcpy(riffID,riffid);
riffSIZE=riffsize;
strcpy(riffFORMAT,riffformat);
}
class FMT{
public:
char fmtID[4]; //FMT标识: "fmt " (含空格)
long fmtSIZE; //块大小(对于PCM16而言)
short wFormatTag; //数字声音的格式
short nChannels; //声道的数量(1为单声道、2为立体声)
long nSamplesPerSec; //每秒样本数
long nAvgBytesPerSec; //每秒平均字节数
short nBlockAlign; //播放的最小字节数
short wBitsPerSample; //每样本位数(8 或16)
public:
FMT(char* ="fmt ",long =16,short=1,short=1,long=8000,long=16000,short=2,short=16);
};
FMT::FMT(char*id,long ssize,short formatag,short cha,long ns,long na,short nb,short wb){
strcpy(fmtID,id);
fmtSIZE=ssize;
wFormatTag=formatag;
nChannels=cha;
nSamplesPerSec=ns;
nAvgBytesPerSec=na;
nBlockAlign=nb;
wBitsPerSample=wb;
}
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTtplayDlg dialog
CTtplayDlg::CTtplayDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTtplayDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTtplayDlg)
m_strFile = _T("");
m_disk = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTtplayDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTtplayDlg)
DDX_Control(pDX, IDC_INPUT_STATUS, m_InStatus);
DDX_Text(pDX, IDC_SAVE_FILE, m_strFile);
DDX_CBString(pDX, IDC_COMBO1_disk, m_disk);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTtplayDlg, CDialog)
//{{AFX_MSG_MAP(CTtplayDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
ON_CBN_EDITCHANGE(IDC_COMBO1_disk, OnEditchangeCOMBO1disk)
ON_NOTIFY(NM_OUTOFMEMORY, IDC_INPUT_STATUS, OnOutofmemoryInputStatus)
ON_BN_CLICKED(IDC_START, OnStart)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTtplayDlg message handlers
BOOL CTtplayDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
// TODO: Add extra initialization here
m_InStatus.SetRange(0, 100);
m_InStatus.SetPos(0);
return TRUE; // return TRUE unless you set the focus to a control
}
void CTtplayDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CTtplayDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTtplayDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTtplayDlg::OnBrowse()
{
CFileDialog Dlg(false);
if (Dlg.DoModal() == IDCANCEL)
return;
m_strFile = Dlg.GetPathName();
// AfxMessageBox(m_disk);
//this->SetDlgItemText(IDC_COMBO1_disk, m_disk);
m_strFile+= ".wav";
UpdateData(false);
}
void CTtplayDlg::OnEditchangeCOMBO1disk()
{
// TODO: Add your control notification handler code here
}
void CTtplayDlg::OnOutofmemoryInputStatus(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
*pResult = 0;
}
void CTtplayDlg::OnStart()
{
UpdateData(true);
if (m_strFile.IsEmpty())
{
AfxMessageBox(_T("请先输入要保存的文件名"));
return;
}
//选择SD卡盘符
char drivename[]="\\\\.\\A:";
char b=m_disk[0];
drivename[4]=b;
int read_sd;
unsigned char buffer[512]={0};
HANDLE hDev=CreateFile(drivename,GENERIC_READ,FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
SetFilePointer(hDev,0,0,FILE_BEGIN);
DWORD dwCB;
read_sd=ReadFile(hDev,buffer,512,&dwCB,0);
// if(read_sd){
// cout<<"读取sd卡成功"<<endl;
// }
// else{
// cout<<"读取sd卡失败"<<endl;
// }
CloseHandle(hDev);
int sd_end=0;//sd卡数据读取完毕标志
int write_sd;
//判断0扇区第1个字节是否为"0x10101010"
if(buffer[0]==170){
sd_end=1;
buffer[0]=0;
HANDLE hsdw=CreateFile(drivename,GENERIC_WRITE,FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
// write_sd=WriteFile(hsdw,buffer,512,&dwCB,0);
CloseHandle(hsdw);
if(write_sd){
AfxMessageBox(_T("开始转换数据"));
}
else{
AfxMessageBox(_T("sd卡重置失败"));
}
//
}
else{
AfxMessageBox(_T("sd卡已被读取"));
sd_end=0;
}
int size_blog=buffer[10]*65536+buffer[9]*256+buffer[8];
int fmtSIZEsd=size_blog*512*4+36;
int datasizeq=size_blog*512*4;
//写入WAV文件头
//写入WAV文件头
//写入WAV文件头
if(sd_end==1){
RIFF wav_riff("RIFF",fmtSIZEsd,"WAVE");
FMT wav_fmt;
long *si=&wav_fmt.fmtSIZE;
short *fo=&wav_fmt.wFormatTag;
short *ch=&wav_fmt.nChannels;
long*nns=&wav_fmt.nSamplesPerSec;
long*nna=&wav_fmt.nAvgBytesPerSec;
short*nnb=&wav_fmt.nBlockAlign;
short*wb=&wav_fmt.wBitsPerSample;
char *jm="data";
HANDLE hDev=CreateFile(m_strFile.GetBuffer(m_strFile.GetLength()),GENERIC_WRITE,FILE_SHARE_WRITE,0,OPEN_ALWAYS,0,0);
SetFilePointer(hDev,0,0,FILE_BEGIN);
long *ll;
ll=&wav_riff.riffSIZE;
WriteFile(hDev,wav_riff.riffID, 4, &dwCB, NULL);//RIFF标识
WriteFile(hDev,ll, 4, &dwCB, NULL);//文件长度减8 字节
WriteFile(hDev,wav_riff.riffFORMAT, 4, &dwCB, NULL);//WAVE标识:"WAVE"
WriteFile(hDev,wav_fmt.fmtID, 4, &dwCB, NULL);//FMT标识: "fmt " (含空格)
WriteFile(hDev,si, 4, &dwCB, NULL);//块大小(对于PCM16而言)
WriteFile(hDev,fo, 2, &dwCB, NULL);//数字声音的格式
WriteFile(hDev,ch, 2, &dwCB, NULL);//声道的数量(1为单声道、2为立体声)
WriteFile(hDev,nns, 4, &dwCB, NULL);//每秒样本数
WriteFile(hDev,nna, 4, &dwCB, NULL);//每秒平均字节数
WriteFile(hDev,nnb, 2, &dwCB, NULL);//播放的最小字节数
WriteFile(hDev,wb, 2, &dwCB, NULL);//每样本位数(8 或16)
WriteFile(hDev,jm, 4, &dwCB, NULL);//date标识
int *ds=&datasizeq;
WriteFile(hDev,ds, 4, &dwCB, NULL);//data块大小
}
//转换数据编码
//signed char adpcm[1024]={0};
strcpy(Info.drivename,drivename);
Info.hDev=hDev;
// Info.mread_sd=mread_sd;
Info.sd_end=sd_end;
Info.size_blog=size_blog;
Info.pctrlProgress=&m_InStatus;
pThread=AfxBeginThread(ThreadFunc,&Info);
}
UINT ThreadFunc(LPVOID lpParam){
threadInfo* pInfo=(threadInfo*)lpParam;
// g726_state state_ptr;
// g726_init_state(&state_ptr)
int sd_end=pInfo->sd_end;
char drivename[7]="\\\\.\\A:";
strcpy(drivename,pInfo->drivename);
int size_blog=pInfo->size_blog;
short pcm[1024];
char sd_adpcm[512]={0};
HANDLE hDev=pInfo->hDev;
DWORD dwCB;
int StartSector=1;//sd卡扇区号
HANDLE mread_sd=CreateFile(drivename,GENERIC_READ,FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
SetFilePointer(mread_sd,(StartSector)*512,0,FILE_BEGIN);
while(sd_end){
ReadFile(mread_sd,sd_adpcm,512,&dwCB,0);
StartSector++;
//
int pp=(StartSector*100)/size_blog;
pInfo->pctrlProgress->SetPos(pp);
// m_InStatus.SetPos(pp);
// m_InStatus.UpdateWindow();
g726_Decode(sd_adpcm,pcm);
if(StartSector==size_blog+1){sd_end=0;AfxMessageBox(_T("数据读取完成"));}
WriteFile(hDev,pcm,2048,&dwCB,NULL);
}
CloseHandle(hDev);
CloseHandle(mread_sd);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -