MCPcopy Create free account
hub / github.com/OpenApoc/OpenApoc / loadSample

Method loadSample

framework/sampleloader/wavsound.cpp:34–99  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

32 public:
33 constexpr explicit WavSampleLoader(const Data &data) : data(data) {}
34 sp<Sample> loadSample(UString path) override
35 {
36 // Raw sound format strings come in the form:
37 // WAV:FILENAME
38 // They are all assumed to be PCK_UINT8 1channel
39
40 const auto splitString = split(path, ":");
41 if (splitString.size() != 2)
42 {
43 LogInfo("String \"%s\" doesn't look like a rawsample - need 2 elements (got %zu)", path,
44 splitString.size());
45 return nullptr;
46 }
47 if (splitString[0] != "WAV")
48 {
49 LogInfo("String \"%s\" doesn't look like a wav - no WAV prefix", path);
50 return nullptr;
51 }
52 const auto file = data.fs.open(splitString[1]);
53 if (!file)
54 {
55 LogWarning("wav \"%s\" failed to open file \"%s\"", path, splitString[1]);
56 return nullptr;
57 }
58
59 const auto fullPath = file.systemPath();
60
61 auto sample = mksp<Sample>();
62 SDL_AudioSpec spec;
63 uint8_t *buf;
64 uint32_t bufSize;
65
66 const auto *returnedSpec = SDL_LoadWAV(fullPath.c_str(), &spec, &buf, &bufSize);
67 if (returnedSpec == nullptr)
68 {
69 LogWarning("Failed to open WAV file at \"%s\" - \"%s\"", fullPath, SDL_GetError());
70 return nullptr;
71 }
72
73 if (spec.channels != 1)
74 {
75 LogWarning("Failed to open WAV file at \"%s\" - only single channel samples supported",
76 fullPath);
77 SDL_FreeWAV(buf);
78 return nullptr;
79 }
80
81 const auto format = sdlFormatToSampleFormat(spec.format);
82 if (!format)
83 {
84 LogWarning("Failed to open WAV file at \"%s\" - unsupported SDL format 0x%08x",
85 fullPath, spec.format);
86 SDL_FreeWAV(buf);
87 return nullptr;
88 }
89
90 sample->format.frequency = spec.freq;
91 sample->format.channels = 1;

Callers

nothing calls this directly

Calls 7

splitFunction · 0.85
sdlFormatToSampleFormatFunction · 0.85
openMethod · 0.80
getSampleSizeMethod · 0.80
sizeMethod · 0.45
resetMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected