File: IAudioLoader.cpp

package info (click to toggle)
jazz2-native 3.5.0-1
  • links: PTS, VCS
  • area: contrib
  • in suites:
  • size: 16,836 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (66 lines) | stat: -rw-r--r-- 2,061 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "IAudioLoader.h"

#if defined(WITH_AUDIO)
#	include "AudioLoaderWav.h"
#	if defined(WITH_VORBIS)
#		include "AudioLoaderOgg.h"
#	endif
#	if defined(WITH_OPENMPT)
#		include "AudioLoaderMpt.h"
#	endif
#endif

#include <IO/FileSystem.h>

using namespace Death::Containers;
using namespace Death::Containers::Literals;
using namespace Death::IO;

namespace nCine
{
	IAudioLoader::IAudioLoader(std::unique_ptr<Stream> fileHandle)
		: hasLoaded_(false), fileHandle_(std::move(fileHandle)), bytesPerSample_(0), numChannels_(0),
			frequency_(0), numSamples_(0L), duration_(0.0f)
	{
	}

	/*std::unique_ptr<IAudioLoader> IAudioLoader::createFromMemory(const unsigned char* bufferPtr, unsigned long int bufferSize)
	{
		// TODO: path cannot be null, otherwise InvalidAudioLoader will be created
		//LOGI("Loading from memory: 0x{:x}, {} bytes", bufferPtr, bufferSize);
		return createLoader(std::make_unique<MemoryStream>(bufferPtr, bufferSize), {});
	}*/

	std::unique_ptr<IAudioLoader> IAudioLoader::createFromFile(const StringView path)
	{
		return createLoader(fs::Open(path, FileAccess::Read), path);
	}

	std::unique_ptr<IAudioLoader> IAudioLoader::createFromStream(std::unique_ptr<Stream> fileHandle, StringView path)
	{
		return createLoader(std::move(fileHandle), path);
	}

	std::unique_ptr<IAudioLoader> IAudioLoader::createLoader(std::unique_ptr<Stream> fileHandle, StringView path)
	{
#if defined(WITH_AUDIO)
		auto extension = fs::GetExtension(path);
		if (extension == "wav"_s) {
			return std::make_unique<AudioLoaderWav>(std::move(fileHandle));
		}
#	if defined(WITH_VORBIS)
		if (extension == "ogg"_s) {
			return std::make_unique<AudioLoaderOgg>(std::move(fileHandle));
		}
#	endif
#	if defined(WITH_OPENMPT)
		if (extension == "it"_s || extension == "j2b"_s || extension == "mo3"_s || extension == "mod"_s || extension == "s3m"_s || extension == "xm"_s) {
			return std::make_unique<AudioLoaderMpt>(std::move(fileHandle));
		}
#	endif

		LOGF("Unknown extension: {}", extension);
#endif
		return std::make_unique<InvalidAudioLoader>();
	}
}