| 48 | } |
| 49 | |
| 50 | CreateAssetResult ImportAudio::Import(CreateAssetContext& context, AudioDecoder& decoder) |
| 51 | { |
| 52 | // Get import options |
| 53 | Options options; |
| 54 | if (context.CustomArg != nullptr) |
| 55 | { |
| 56 | // Copy import options from argument |
| 57 | options = *static_cast<Options*>(context.CustomArg); |
| 58 | } |
| 59 | else |
| 60 | { |
| 61 | // Restore the previous settings or use default ones |
| 62 | if (!TryGetImportOptions(context.TargetAssetPath, options)) |
| 63 | { |
| 64 | LOG(Warning, "Missing audio import options. Using default values."); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // Vorbis uses fixed 16-bit depth |
| 69 | if (options.Format == AudioFormat::Vorbis) |
| 70 | options.BitDepth = AudioTool::BitDepth::_16; |
| 71 | |
| 72 | LOG_STR(Info, options.ToString()); |
| 73 | |
| 74 | // Open the file |
| 75 | auto stream = FileReadStream::Open(context.InputPath); |
| 76 | if (stream == nullptr) |
| 77 | return CreateAssetResult::InvalidPath; |
| 78 | DeleteMe<FileReadStream> deleteStream(stream); |
| 79 | |
| 80 | // Load the audio data |
| 81 | AudioDataInfo info; |
| 82 | Array<byte> audioData; |
| 83 | if (decoder.Convert(stream, info, audioData)) |
| 84 | return CreateAssetResult::Error; |
| 85 | LOG(Info, "Audio: {0}kHz, channels: {1}, Bit depth: {2}, Length: {3}s", info.SampleRate / 1000.0f, info.NumChannels, info.BitDepth, info.GetLength()); |
| 86 | |
| 87 | // Load the whole audio data |
| 88 | uint32 bytesPerSample = info.BitDepth / 8; |
| 89 | uint32 bufferSize = info.NumSamples * bytesPerSample; |
| 90 | DataContainer<byte> sampleBuffer; |
| 91 | sampleBuffer.Link(audioData.Get()); |
| 92 | |
| 93 | // Convert bit depth if need to |
| 94 | uint32 outputBitDepth = (uint32)options.BitDepth; |
| 95 | if (outputBitDepth != info.BitDepth) |
| 96 | { |
| 97 | const uint32 outBufferSize = info.NumSamples * (outputBitDepth / 8); |
| 98 | sampleBuffer.Allocate(outBufferSize); |
| 99 | AudioTool::ConvertBitDepth(audioData.Get(), info.BitDepth, sampleBuffer.Get(), outputBitDepth, info.NumSamples); |
| 100 | info.BitDepth = outputBitDepth; |
| 101 | bytesPerSample = info.BitDepth / 8; |
| 102 | bufferSize = outBufferSize; |
| 103 | } |
| 104 | |
| 105 | // Base |
| 106 | IMPORT_SETUP(AudioClip, AudioClip::SerializedVersion); |
| 107 | uint32 samplesPerChunk[ASSET_FILE_DATA_CHUNKS]; |
nothing calls this directly
no test coverage detected