Author Topic: C++ Bass.dll 'synth' problem  (Read 413 times)

bnb632

  • Posts: 3
C++ Bass.dll 'synth' problem
« on: 26 Dec '17 - 20:06 »
Hi everyone,
so I try to put project synth to class, and after I do like this,
DWORD CALLBACK Sound::WriteStream(HSTREAM handle, short *buffer, DWORD length, void *user);
and in Sound class I have, initialization function with this code. 
stream = BASS_StreamCreate(info.freq, 2, 0, (STREAMPROC*)WriteStream, 0);
When I try to compile this project I have a error like,
invalid type conversation . "(STREAMPROC*)WriteStream".
What I do wrong  ?

saga

  • Posts: 2269
Re: C++ Bass.dll 'synth' problem
« Reply #1 on: 26 Dec '17 - 22:58 »
C++ class members does not follow the required calling convention due to the implicit "this" parameter. In addition, "short *buffer" is not the same type as "void *buffer" obviously. Make your callback function static, and if you really need access to the object state, pass your class object as a user parameter. You can also do something like this if you want a class member function:

Code: [Select]
class Sound
{

// Static wrapper, to be passed as a callback to BASS
static DWORD CALLBACK WriteStream(HSTREAM handle, void *buffer, DWORD length, void *user)
{
    return static_cast<Sound *>(user)->WriteStream(handle, static_cast<short *>(buffer), length)
}

DWORD CALLBACK WriteStream(HSTREAM handle, short *buffer, DWORD length)
{
// do real processing stuff here
}


};

Sound sound;
HSTREAM stream=BASS_StreamCreate(44100, 2, 0, Sound::WriteStream, &sound);

Never ever cast function pointers. If you have to cast them, you are most likely doing something wrong, and the compiler is trying to save you from that.
« Last Edit: 26 Dec '17 - 23:02 by saga »

bnb632

  • Posts: 3
Re: C++ Bass.dll 'synth' problem
« Reply #2 on: 27 Dec '17 - 09:24 »
@Saga : so after compile I have problem :
return static_cast<Sound *>(user)->WriteStream(handle, static_cast<short *>(buffer), length);
here :
(user)->WriteStream <- no instance of overloaded function "Sound::WriteStream" matches the argument list: argument types are : (HSTREAM, short*, DWORD)
object type is:Sound
Do you know how to resolve this ?
Sorry for my problem, but I have no idea how to use this library.

Ian @ un4seen

  • Administrator
  • Posts: 21358
Re: C++ Bass.dll 'synth' problem
« Reply #3 on: 27 Dec '17 - 16:32 »
Did you include the 2nd WriteStream function (as in saga's post) in your "Sound" class? It should prevent that error message.

bnb632

  • Posts: 3
Re: C++ Bass.dll 'synth' problem
« Reply #4 on: 28 Dec '17 - 12:24 »
Did you include the 2nd WriteStream function (as in saga's post) in your "Sound" class? It should prevent that error message.
Yes, i did. I include this like that.
Sound.h
Code: [Select]
static DWORD CALLBACK Sound::WriteStream(HSTREAM handle, void *buffer, DWORD length, void *user)
{
return static_cast<Sound *>(user)->WriteStream(handle, static_cast<short *>(buffer), length);
}
DWORD WriteStream(HSTREAM handle, short * buffer, DWORD length, void * user);
Sound();
and the same thing i Sound.cpp but, Visual studio show me the error "(user)->WriteStream" here. Message: no instance of overloaded function "Sound::WriteStream" matches the argument list: argument types are : (HSTREAM, short*, DWORD)
object type is:Sound"

Ian @ un4seen

  • Administrator
  • Posts: 21358
Re: C++ Bass.dll 'synth' problem
« Reply #5 on: 28 Dec '17 - 17:35 »
You don't need the "user" parameter in the 2nd "WriteStream" function.