ValveSoftware / steam-audio

Steam Audio
https://valvesoftware.github.io/steam-audio/
Apache License 2.0
2.2k stars 152 forks source link

Compilation issue using C api #269

Closed DalisonCPU closed 10 months ago

DalisonCPU commented 10 months ago

Hello. I downloaded the latest version of steamaudio from the page https://github.com/ValveSoftware/steam-audio/releases I downloaded the file steamaudio_4.3.0.zip, and I can't compile a project because it says IPLhandle not found. My question is: How can I correctly use the api, since the steamaudio/phonon.h file doesn't really have this declaration?

lakulish commented 10 months ago

Can you provide more details or an example of the code you're trying to compile? Which version of the Steam Audio API is your code targeting? IPLhandle is part of the older v2.x API, so there appears to be some sort of version mismatch.

DalisonCPU commented 10 months ago

Hello, i'm using the latest version of the api (4.3), and would like to compile the following code for Windows x86: P.S: I know I must be doing something wrong, but I can't find how to apply the espatilizer effect to an audio buffer and when I try to compile it says that IPLhandle was not declared.

Click to expand ``` //Descomente as duas próximas linhas se compilar com o visual studio... #pragma comment(lib, "libs/bass.lib") #pragma comment(lib, "libs/phonon.lib") #include #include #include #include #include #include "bass.h" #include "steamaudio/phonon.h" using namespace std; #define waitms(ms) this_thread::sleep_for(chrono::milliseconds(ms)) #define getLength(handle) BASS_ChannelBytes2Seconds(handle, BASS_ChannelGetLength(handle, BASS_POS_BYTE)) bool decodeAudioFile(const string &filename, vector &buffer); HSTREAM hstreamFromPCM(const vector &buffer); void spatialize(vector &inputaudio, vector &outdata); int main() {     string filename = "music.mp3";     BASS_Init(-1, 44100, 0, NULL, 0);     vector buffer;//Buffer original antes de passar pela spatialize...     vector final;//Os dados depois de passar pela spatialize... //Decodifica o arquivo mp3... cout<<"Decodificando "< 0)     {         cout << fixed; cout<<"Stream criado com sucesso!"< &buffer) {     HSTREAM handle = BASS_StreamCreateFile(FALSE, filename.c_str(), 0, 0, BASS_SAMPLE_FLOAT | BASS_STREAM_DECODE);     if (handle == 0)     {         cout << "Cannot open the file..." << filename << endl;         return false;     }     BASS_ChannelPlay(handle, FALSE);     vector arr;     arr.resize(512);     size_t len = 512 * sizeof(float);     while (BASS_ChannelIsActive(handle) == BASS_ACTIVE_PLAYING)     {         size_t size = BASS_ChannelGetData(handle, arr.data(), len);         if (size > 0)         {             buffer.insert(buffer.end(), arr.begin(), arr.begin() + (size / sizeof(float)));         }     }     BASS_StreamFree(handle);     return true; } // Create a stream from a floating point pcm data buffer... HSTREAM hstreamFromPCM(const vector &buffer) {     if (buffer.size() == 0)         return 0;     HSTREAM handle = BASS_StreamCreate(44100, 2, BASS_SAMPLE_FLOAT, STREAMPROC_PUSH, NULL);     if (handle > 0)     {         if (BASS_StreamPutData(handle, buffer.data(), buffer.size() * sizeof(float)) <= 0)         {             cout << "Cannot use BASS_StreamPutData... " << BASS_ErrorGetCode() << endl;             BASS_StreamFree(handle);             handle = 0;         }     }     return handle; } //Função problemática... //A ideia aqui é passar os dados pcm, aplicar o efeito e retornar o resultado... //É do exemplo oficial em: //https://valvesoftware.github.io/steam-audio/doc/capi/getting-started.html void spatialize(vector &inputaudio, vector &outdata) {     // Inicialização do Steam Audio     IPLhandle context{nullptr};     iplContextCreate(nullptr, nullptr, nullptr, &context);     auto const samplingrate = 44100;     auto const framesize = 1024;     IPLRenderingSettings settings{ samplingrate, framesize };     IPLhandle renderer{ nullptr };     IPLHrtfParams hrtfParams{ IPL_HRTFDATABASETYPE_DEFAULT, nullptr, 0, nullptr, nullptr, nullptr };     iplCreateBinauralRenderer(context, settings, hrtfParams, &renderer);     IPLAudioFormat mono;     mono.channelLayoutType  = IPL_CHANNELLAYOUTTYPE_SPEAKERS;     mono.channelLayout      = IPL_CHANNELLAYOUT_MONO;     mono.channelOrder       = IPL_CHANNELORDER_INTERLEAVED;     IPLAudioFormat stereo;     stereo.channelLayoutType  = IPL_CHANNELLAYOUTTYPE_SPEAKERS;     stereo.channelLayout      = IPL_CHANNELLAYOUT_STEREO;     stereo.channelOrder       = IPL_CHANNELORDER_INTERLEAVED;     IPLhandle effect{ nullptr };     iplBinauralEffectCreate(renderer, mono, stereo, &effect);     // Processamento do áudio (exemplo do loop principal)     std::vector outputaudioframe(2 * framesize);     std::vector outputaudio;     IPLAudioBuffer inbuffer{ mono, framesize, inputaudio.data() };     IPLAudioBuffer outbuffer{ stereo, framesize, outputaudioframe.data() };     auto numframes = static_cast(inputaudio.size() / framesize);     for (auto i = 0; i < numframes; ++i)     {         iplBinauralEffectApply(effect, inbuffer, IPLVector3{ 1.0f, 1.0f, 1.0f }, IPL_HRTFINTERPOLATION_NEAREST, outbuffer);         std::copy(std::begin(outputaudioframe), std::end(outputaudioframe), std::back_inserter(outputaudio));         inbuffer.interleavedBuffer += framesize;     }     // Limpeza     iplBinauralEffectRelease(&effect);     iplDestroyBinauralRenderer(&renderer);     iplContextRelease(&context);     iplCleanup();     // Copiando o áudio processado para a saída     outdata.resize(0);     std::copy(outputaudio.begin(), outputaudio.end(), back_inserter(outdata)); } ```

Sorry for the comments in Portuguese.

lakulish commented 10 months ago

It definitely looks like the code is written for the old (v2.x) API, but is being compiled against the latest (v4.3.0) API. You will probably have to change the code to use the new API. For example, when creating a context, you need to call iplContextCreate instead of iplCreateContext, and pass it an IPLContext instead of an IPLhandle. For a full example with the latest API, see https://valvesoftware.github.io/steam-audio/doc/capi/getting-started.html#full-program-listing.

DalisonCPU commented 10 months ago

Ok, but even the api example, how do I reproduce the result of it? The inputaudio.raw could be a .wav file, with 44100 and 128 kbps, right? If so, when I do that it generates a file and if I open it in aldacity, it sounds completely strange and has nothing to do with the original audio. Could you give a little more information?

achandak commented 10 months ago

Steam Audio accepts 32-bit floating point samples when creating Audio Buffers. You can load those buffers however you want, from a wav or mp3 file, but you will have to figure out how to load a wav or mp3 file.

For simplicity, I think the examples assumes a raw file which has basically a float array of audio buffer data.