Rise
The Vieneo Province
Loading...
Searching...
No Matches
OwnedAudioStream.h
Go to the documentation of this file.
1#pragma once
2
3#include "bass.h"
4#include <cstddef>
5#include <cstring>
6#include <memory>
7
9{
10 inline void CALLBACK FreeMemory(HSYNC, DWORD, DWORD, void* memory)
11 {
12 delete[] static_cast<unsigned char*>(memory);
13 }
14
15 // BASS 2.4.17 reads memory streams in place. Network packet storage goes away
16 // after the packet is processed, so give the stream its own immutable copy.
17 // The FREE sync also covers BASS_Free/device changes and deferred stream frees.
18 inline HSTREAM Create(const unsigned char* data, std::size_t length, DWORD flags, int& error)
19 {
20 error = BASS_OK;
21 if (!data || length == 0)
22 {
23 error = BASS_ERROR_ILLPARAM;
24 return 0;
25 }
26 auto memory = std::make_unique<unsigned char[]>(length);
27 std::memcpy(memory.get(), data, length);
28 const HSTREAM stream = BASS_StreamCreateFile(TRUE, memory.get(), 0, length, flags);
29 if (!stream)
30 {
31 error = BASS_ErrorGetCode();
32 return 0;
33 }
34 if (!BASS_ChannelSetSync(stream, BASS_SYNC_FREE, 0, FreeMemory, memory.get()))
35 {
36 error = BASS_ErrorGetCode();
37 // No playback/callbacks have started, so this free is synchronous.
38 // If it unexpectedly fails, retain storage rather than leave BASS
39 // holding a dangling pointer.
40 if (!BASS_StreamFree(stream)) memory.release();
41 return 0;
42 }
43 memory.release(); // owned by the stream's FREE callback from here on
44 return stream;
45 }
46}
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
void CALLBACK FreeMemory(HSYNC, DWORD, DWORD, void *memory)
HSTREAM Create(const unsigned char *data, std::size_t length, DWORD flags, int &error)