| 47 | } |
| 48 | |
| 49 | bool LoadAudioFile(const char *path, bool stream, bool errorDialog, SoundSample &result) |
| 50 | { |
| 51 | bool isMp3 = true; |
| 52 | std::string foundPath = GetMp3Path(path); |
| 53 | AssetRef ref = FindAsset(foundPath.c_str()); |
| 54 | if (!ref.ok()) { |
| 55 | ref = FindAsset(path); |
| 56 | foundPath = path; |
| 57 | isMp3 = false; |
| 58 | } |
| 59 | if (!ref.ok()) |
| 60 | ErrDlg("Audio file not found", StrCat(path, "\n", SDL_GetError(), "\n"), __FILE__, __LINE__); |
| 61 | |
| 62 | #ifdef STREAM_ALL_AUDIO_MIN_FILE_SIZE |
| 63 | #if STREAM_ALL_AUDIO_MIN_FILE_SIZE == 0 |
| 64 | stream = true; |
| 65 | #else |
| 66 | size_t size; |
| 67 | if (!stream) { |
| 68 | size = ref.size(); |
| 69 | stream = size >= STREAM_ALL_AUDIO_MIN_FILE_SIZE; |
| 70 | } |
| 71 | #endif |
| 72 | #endif |
| 73 | |
| 74 | if (stream) { |
| 75 | if (result.SetChunkStream(foundPath, isMp3, /*logErrors=*/true) != 0) { |
| 76 | if (errorDialog) { |
| 77 | ErrDlg("Failed to load audio file", StrCat(foundPath, "\n", SDL_GetError(), "\n"), __FILE__, __LINE__); |
| 78 | } |
| 79 | return false; |
| 80 | } |
| 81 | } else { |
| 82 | #if !defined(STREAM_ALL_AUDIO_MIN_FILE_SIZE) || STREAM_ALL_AUDIO_MIN_FILE_SIZE == 0 |
| 83 | const size_t size = ref.size(); |
| 84 | #endif |
| 85 | AssetHandle handle = OpenAsset(std::move(ref)); |
| 86 | if (!handle.ok()) { |
| 87 | if (errorDialog) |
| 88 | ErrDlg("Failed to load audio file", StrCat(foundPath, "\n", SDL_GetError(), "\n"), __FILE__, __LINE__); |
| 89 | return false; |
| 90 | } |
| 91 | auto waveFile = MakeArraySharedPtr<std::uint8_t>(size); |
| 92 | if (!handle.read(waveFile.get(), size)) { |
| 93 | if (errorDialog) |
| 94 | ErrDlg("Failed to read file", StrCat(foundPath, ": ", SDL_GetError()), __FILE__, __LINE__); |
| 95 | return false; |
| 96 | } |
| 97 | const int error = result.SetChunk(waveFile, size, isMp3); |
| 98 | if (error != 0) { |
| 99 | if (errorDialog) |
| 100 | ErrSdl(); |
| 101 | return false; |
| 102 | } |
| 103 | } |
| 104 | return true; |
| 105 | } |
| 106 | |