How to play MIDI with bassmidi?(C++) - c++

There's no sound.
Here's my code:
::BASS_Init(-1, 44100, 0, 0, 0);
BGM = ::BASS_MIDI_StreamCreateFile(FALSE, file, 0, 0, NULL, NULL);
BASS_ChannelSetAttribute(BGM, BASS_ATTRIB_MIDI_VOL, BGMVolume);
::BASS_ChannelFlags(BGM, BASS_SAMPLE_LOOP, BASS_SAMPLE_LOOP);
::BASS_ChannelPlay(BGM, true);
I can play a mp3 when using:
BGM = ::BASS_StreamCreateFile(FALSE, file, 0, 0, NULL);
BASS_ChannelSetAttribute(BGM, BASS_ATTRIB_VOL, BGMVolume);

Related

SetWinEventHook and GetMessage c++

I would like to set some events with SetWinEventHook and wait.
After five events I want to exit from the message loop. The problem is that the code stucks in getMessage without entering in the loop.
The Callback function is correctly called whenever an event occurred.
HWINEVENTHOOK hEvent = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, NULL,
WinEventProcCallback, 0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);
HWINEVENTHOOK hWinEventHook0 = SetWinEventHook(
EVENT_OBJECT_CREATE, EVENT_OBJECT_CREATE,
NULL, WinEventProcCallback, 0, 0,
WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);
HWINEVENTHOOK hWinEventHook1 = SetWinEventHook(
EVENT_OBJECT_DESTROY, EVENT_OBJECT_DESTROY,
NULL, WinEventProcCallback, 0, 0,
WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);
while (ret = GetMessage(&msg, NULL, 0, 0) > 0) {
if (ret == -1)
{
return;
}
if (cont >= 5) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
cont++;
std::cout << "Reached : " << cont << std::endl;
}
How I can solve this?
Thank you in advance
if (ret = GetMessage(&msg, NULL, 0, 0) > 0)
is equal to
if (ret = (GetMessage(&msg, NULL, 0, 0) > 0))
while (ret = GetMessage(&msg, NULL, 0, 0) > 0) {
if (ret == -1)
{
return;
}
the if statement won't be true in this case, so you won't get to the return.
ret can't be greater than 0 and -1 at the same time

Not all work-items being used opencl

so I'm able to compile and execute my kernel, the problem is that only two work-items are being used. I'm basically trying to fill up a float array[8] with {0,1,2,3,4,5,6,7}. So this is a very simple hello world application. Bellow is my kernel.
// Highly simplified to demonstrate
__kernel void rnd_float32_matrix (
__global float * res
) {
uint idx = get_global_id(0);
res[idx] = idx;
}
I then create and execute the kernel with the following code...
// Some more code
cl::Program program(context, sources, &err);
program.build(devices, NULL, NULL, NULL);
cl::Kernel kernel(program, "rnd_float32_matrix", &err);
kernel.setArg(0, src_d);
cl::CommandQueue queue(context, devices[0], 0, &err);
cl::Event event;
err = queue.enqueueNDRangeKernel(
kernel,
cl::NullRange,
cl::NDRange(8),
// I've tried cl::NDRange(8) as well
cl::NDRange(1),
NULL,
&event
);
event.wait();
err = queue.enqueueReadBuffer(
// This is:
// cl::Buffer src_d(
// context,
// CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
// mem_size,
// src_h,
// &err);
src_d,
CL_TRUE,
0,
8,
// This is float * src_h = new float[8];
src_h);
for(int i = 0; i < 8; i ++) {
std::cout << src_h[i] << std::endl;
}
I may not show it in the code, but I also do select a gpu device and using context.getInfo(..) it shows I'm using my NVidia GTX 770M card which shows 1024, 1024, 64 work-items available in dimensions 0, 1 and 2. When this array prints I keep getting... 0, 1, 0, 0, 0, 0, 0, 0. I've also tried setting res[idx] = 5, and I get... 5, 5, 0, 0, 0, 0, 0, 0. So it seems that only two give work-items are actually being used. What am I doing wrong?
Your command to read the data back from the device is only reading 8 bytes, which is two floats:
err = queue.enqueueReadBuffer(
src_d,
CL_TRUE,
0,
8, // <- This is the number of bytes, not the number of elements!
// This is float * src_h = new float[8];
src_h);
To read 8 floats, you would need to do this:
err = queue.enqueueReadBuffer(
src_d,
CL_TRUE,
0,
8 * sizeof(cl_float),
// This is float * src_h = new float[8];
src_h);

Serialize string to UTF w/o BOM

I'm trying to code unicode string serialization to UTF-8 w/o BOM file. For some reason the code below gives wrong output.
static void MyWriteFile(HANDLE hFile, PTCHAR pszText, int cchLen, BOOL bAsUnicode)
{
DWORD dwBytes;
size_t utf8len = WideCharToMultiByte(CP_UTF8, 0, pszText, -1, NULL, 0, NULL, NULL);
PCHAR pszConverted = (PCHAR)LocalAlloc(LPTR, utf8len);
WideCharToMultiByte(CP_UTF8, 0, pszText, utf8len, pszConverted, utf8len, 0, 0);
WriteFile(hFile, pszConverted, utf8len, &dwBytes, NULL);
}
WideCharToMultiByte(CP_UTF8, 0, pszText, utf8len, pszConverted, utf8len, 0, 0);
The fourth parameter of WideCharToMultiByte (cchWideChar) is the size of the input string. You should leave that to -1 as it is null terminated. Otherwise your output buffer is probably not large enough, and it will contain too much data.

Redirecting Stdout of a commandline process to a file

I have a MFC application which send a command to cmd through CreateProcess function. I want to get the stdout into a log file. The code is like this
void CMainFrame::OnCompile( CString cmd )
{
CWaitCursor cursor;
SECURITY_ATTRIBUTES sec;
ZeroMemory( &sec, sizeof(sec) );
sec.nLength = sizeof(SECURITY_ATTRIBUTES);
sec.lpSecurityDescriptor = NULL;
sec.bInheritHandle = TRUE;
HANDLE hstdoutf = CreateFile("e:\\user_stdout.txt",GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,&sec,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
STARTUPINFO siStartupInfo;
PROCESS_INFORMATION piProcessInfo;
memset(&siStartupInfo, 0, sizeof(siStartupInfo));
memset(&piProcessInfo, 0, sizeof(piProcessInfo));
siStartupInfo.cb = sizeof(siStartupInfo);
siStartupInfo.dwFlags = STARTF_USESHOWWINDOW ;
siStartupInfo.wShowWindow = SW_HIDE;
if (hstdoutf!=INVALID_HANDLE_VALUE) {
siStartupInfo.hStdOutput=hstdoutf;
}
// Wait up to 100 seconds for the compilation process to finish
if (!::CreateProcess(0, (char*)(cmd.GetString()), 0, 0, false,0, 0, 0,
&siStartupInfo, &piProcessInfo) ||
(WaitForSingleObject(piProcessInfo.hProcess, 100000) != WAIT_OBJECT_0))
{
MessageBox("Fail to execute command","Test",MB_ICONERROR);
}
else
{
CloseHandle(hstdoutf);
}
}
but the code is not working properly . Only a blank file is created.
What i am doing wrong ? Please help.
You forgot to speficy the STARTF_USESTDHANDLES flag in siStartupInfo.dwFlags.
Change
startupInfo.dwFlags = STARTF_USESHOWWINDOW ;
to
startupInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES ;
and change
CreateProcess(0, (char*)(cmd.GetString()), 0, 0, false, 0, 0, 0,
&siStartupInfo, &piProcessInfo)
to
CreateProcess(0, (char*)(cmd.GetString()), 0, 0, true, 0, 0, 0,
&siStartupInfo, &piProcessInfo)
I suggest you read the STARTF_USESTDHANDLES section of the STARTUPINFO documentation, there is plenty of information.
BTW you should also add
CloseProcess(piProcessInfo.hProcess)
at the end.

UnmapViewOfFile does not release file. Unable to access File after function is executed.

I am trying to upload a file to a cloud service using winhttp, however after running the below code, i am unable to access the same file anymore. It seems like the program is not closing its file handle properly:
HANDLE hFile = CreateFileW(MultiByteToWideChar(filePath), GENERIC_READ, FILE_SHARE_READ+FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwFileSize = GetFileSize(hFile, NULL);
HANDLE hFileMappingObject = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, dwFileSize, NULL);
HANDLE file = MapViewOfFile(hFileMappingObject, FILE_MAP_READ, 0, 0, 0);
bool result = WinHttpSendRequest( hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS,
0, WINHTTP_NO_REQUEST_DATA, 0,
dwFileSize, 0);
result = WinHttpWriteData( hRequest, file, dwFileSize, &dwBytesWritten);
FlushViewOfFile(file, dwFileSize);
FlushFileBuffers(hFile);
UnmapViewOfFile(file);
CloseHandle(hFileMappingObject);
CloseHandle(hFile);