|
-
July 25th, 2010, 09:32 AM
#16
Re: Printing to Default Printer
Something like:
Code:
for (int i=0; i<lpEvent->nData; ++i)
{
cout << "char " << i << " = " << lpEvent->lpData[i]
<< " = hex = " << hex << (int)lpEvent->lpData[i] << endl;
}
-
July 25th, 2010, 12:03 PM
#17
Re: Printing to Default Printer
Works perfect!
I coudn't get the cout to work, but I did it with fprintf.
Thanks!
-
July 28th, 2010, 02:47 AM
#18
Re: Printing to Default Printer
What was the problem with cout?
Did you include <iostream>?
Did you put "using namespace std;" at the top of the CPP file?
-
August 10th, 2010, 06:55 PM
#19
Re: Printing to Default Printer
I am making progress with printing to a file, but have run into a problem that I don't understand.
I am trying to read a MIDI file and print out the contents.
The code below works for standard type 0 MIDI files (these have only 1 track).
If I use the same code, but open a type 1 MIDI file (these have more than 1 track), the program crashes.
Can someone spot my error?
Code:
#include <windows.h>
#pragma comment (lib, "winmm.lib")
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct EVENT {
BYTE state;
BYTE data1;
BYTE data2;
BYTE type;
int nData;
LPBYTE lpData;
DWORD dwDelta;
struct EVENT *lpNext;
};
typedef struct EVENT EVENT;
typedef struct EVENT *LPEVENT;
HANDLE g_hheap = NULL;
BOOL ReadMidiFile(LPTSTR lpszFileName);
BOOL ReadTrack(HMMIO hmmio, LPEVENT *lplpEvent);
void ReadAndReverse(HMMIO hmmio, LPVOID lpData, DWORD dwSize);
void ReadDelta(HMMIO hmmio, LPDWORD lpdwDelta);
LPVOID Alloc(DWORD dwSize);
BOOL PrintOut(LPEVENT lpEvent);
int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpszCmdLine, int nCmdShow)
{
g_hheap = HeapCreate(0, 4096, 0);
if (g_hheap == NULL)
return 0;
if (!ReadMidiFile(TEXT("SMFtype0.mid")))
//if (!ReadMidiFile(TEXT("SMFtype1.mid")))
MessageBox(NULL, TEXT("error"), NULL, MB_ICONWARNING);
HeapDestroy(g_hheap);
return 0;
}
BOOL ReadMidiFile(LPTSTR lpszFileName)
{
HMMIO hmmio;
WORD i;
WORD wTrack;
WORD wFormat;
WORD wTime;
DWORD dwMagic;
DWORD dwDataLen;
LPEVENT *lplpEvent;
hmmio = mmioOpen(lpszFileName, NULL, MMIO_READ);
if (hmmio == NULL) {
MessageBox(NULL, TEXT("error"), NULL, MB_ICONWARNING);
return FALSE;
}
mmioRead(hmmio, (HPSTR)&dwMagic, sizeof(DWORD));
if (dwMagic != *(LPDWORD)"MThd") {
mmioClose(hmmio, 0);
return FALSE;
}
ReadAndReverse(hmmio, &dwDataLen, sizeof(DWORD));
if (dwDataLen != 6) {
mmioClose(hmmio, 0);
return FALSE;
}
ReadAndReverse(hmmio, &wFormat, sizeof(WORD));
ReadAndReverse(hmmio, &wTrack, sizeof(WORD));
ReadAndReverse(hmmio, &wTime, sizeof(WORD));
lplpEvent = (LPEVENT *)Alloc(sizeof(DWORD) * wTrack);
for (i = 0; i < wTrack; i++) {
if (!ReadTrack(hmmio, &lplpEvent[i])) {
MessageBox(NULL, TEXT("error"), NULL, MB_ICONWARNING);
mmioClose(hmmio, 0);
return FALSE;
}
}
MessageBox(NULL, TEXT("ALL MIDI tracks read"), TEXT("OK"), MB_OK);
mmioClose(hmmio, 0);
return TRUE;
}
BOOL ReadTrack(HMMIO hmmio, LPEVENT *lplpEvent)
{
BYTE statePrev = 0;
DWORD dwLen;
DWORD dwMagic;
LPEVENT lpEvent;
mmioRead(hmmio, (HPSTR)&dwMagic, sizeof(DWORD));
if (dwMagic != *(LPDWORD)"MTrk")
return FALSE;
ReadAndReverse(hmmio, &dwLen, sizeof(DWORD));
lpEvent = (LPEVENT)Alloc(sizeof(EVENT));
*lplpEvent = lpEvent;
for (;;) {
ReadDelta(hmmio, &lpEvent->dwDelta);
mmioRead(hmmio, (HPSTR)&lpEvent->state, sizeof(BYTE));
if (!(lpEvent->state & 0x80)) {
lpEvent->state = statePrev;
mmioSeek(hmmio, -1, SEEK_CUR);
}
switch (lpEvent->state & 0xF0) {
case 0x80:
case 0x90:
case 0xA0:
case 0xB0:
case 0xE0:
mmioRead(hmmio, (HPSTR)&lpEvent->data1, sizeof(BYTE));
mmioRead(hmmio, (HPSTR)&lpEvent->data2, sizeof(BYTE));
break;
case 0xC0:
case 0xD0:
mmioRead(hmmio, (HPSTR)&lpEvent->data1, sizeof(BYTE));
lpEvent->data2 = 0;
break;
case 0xF0:
if (lpEvent->state == 0xF0) {
mmioRead(hmmio, (HPSTR)&lpEvent->nData, sizeof(BYTE));
lpEvent->lpData = (LPBYTE)Alloc(lpEvent->nData + 1);
lpEvent->lpData[0] = lpEvent->state;
mmioRead(hmmio, (HPSTR)(lpEvent->lpData + 1), lpEvent->nData);
lpEvent->nData++;
}
else if (lpEvent->state == 0xFF) {
DWORD dw;
DWORD tmp;
mmioRead(hmmio, (HPSTR)&lpEvent->type, sizeof(BYTE));
dw = (DWORD)-1;
switch (lpEvent->type) {
case 0x00: dw = 2; break;
case 0x01:
case 0x02:
case 0x03:
case 0x04:
case 0x05:
case 0x06:
case 0x07: break;
case 0x20: dw = 1; break;
case 0x21: dw = 1; break;
case 0x2F: dw = 0; break;
case 0x51: dw = 3; break;
case 0x54: dw = 5; break;
case 0x58: dw = 4; break;
case 0x59: dw = 2; break;
case 0x7F: break;
default:
MessageBox(NULL, TEXT("error"), NULL, MB_ICONWARNING);
return FALSE;
}
tmp = dw;
if (dw != -1) {
ReadDelta(hmmio, &dw);
if (dw != tmp) {
MessageBox(NULL, TEXT("error"), NULL, MB_ICONWARNING);
return FALSE;
}
}
else
ReadDelta(hmmio, &dw);
lpEvent->nData = dw;
lpEvent->lpData = (LPBYTE)Alloc(lpEvent->nData);
mmioRead(hmmio, (HPSTR)lpEvent->lpData, lpEvent->nData);
if (lpEvent->type == 0x2F)
return TRUE;
}
else
;
break;
default:
MessageBox(NULL, TEXT("error"), NULL, MB_ICONWARNING);
return FALSE;
}
PrintOut((LPEVENT)lpEvent);
statePrev = lpEvent->state;
lpEvent->lpNext = (LPEVENT)Alloc(sizeof(EVENT));
lpEvent = lpEvent->lpNext;
if (lpEvent == NULL)
break;
}
return FALSE;
}
void ReadAndReverse(HMMIO hmmio, LPVOID lpData, DWORD dwSize)
{
BYTE i;
BYTE tmp;
LPBYTE lp = (LPBYTE)lpData;
LPBYTE lpTail = lp + dwSize - 1;
mmioRead(hmmio, (HPSTR)lp, dwSize);
for (i = 0; i < dwSize / 2; i++) {
tmp = *lp;
*lp = *lpTail;
*lpTail = tmp;
lp++;
lpTail--;
}
}
void ReadDelta(HMMIO hmmio, LPDWORD lpdwDelta)
{
int i;
BYTE tmp;
*lpdwDelta = 0;
for (i = 0; i < sizeof(DWORD); i++) {
mmioRead(hmmio, (HPSTR)&tmp, sizeof(BYTE));
*lpdwDelta = ( (*lpdwDelta) << 7 ) | (tmp & 0x7F);
if (!(tmp & 0x80))
break;
}
}
LPVOID Alloc(DWORD dwSize)
{
return HeapAlloc(g_hheap, HEAP_ZERO_MEMORY, dwSize);
}
BOOL PrintOut(LPEVENT lpEvent)
{
FILE *fp;
const char *str = "--------";
fp = fopen("printout.txt", "a");
fprintf(fp,"%s\n", str);
fprintf(fp,"lpEvent->dwDelta = %d\n", lpEvent->dwDelta);
fprintf(fp,"lpEvent->state = %X\n", lpEvent->state);
fprintf(fp,"lpEvent->type = %X\n", lpEvent->type);
fprintf(fp,"lpEvent->data1 = %X\n", lpEvent->data1);
fprintf(fp,"lpEvent->data2 = %X\n", lpEvent->data2);
fprintf(fp,"lpEvent->nData = %d\n", lpEvent->nData);
fprintf(fp,"lpEvent->lpData = <%s>\n", lpEvent->lpData);
if (lpEvent->lpData[0] == 0xF0)
{
for (int i=0; i<lpEvent->nData; ++i)
{
fprintf(fp," char = <%X>\n", (int)lpEvent->lpData[i]);
}
}
return TRUE;
}
-
August 11th, 2010, 06:46 AM
#20
Re: Printing to Default Printer
I should add, the program operates properly for both type 0 and type 1 MIDI files,
it is just my printing code that causes problems.
-
August 12th, 2010, 12:22 AM
#21
Re: Printing to Default Printer
hi,
we should add, the program operates properly for both type 0 and type 1 MIDI files,
it is just my printing code that causes problems.
regards,
phe9oxis,
http://www.guidebuddha.com
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|