Perfected Audio
This commit is contained in:
@ -3,6 +3,7 @@
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
add_subdirectory(audio)
|
||||
add_subdirectory(display)
|
||||
add_subdirectory(file)
|
||||
add_subdirectory(locale)
|
15
src/dawntools/audio/CMakeLists.txt
Normal file
15
src/dawntools/audio/CMakeLists.txt
Normal file
@ -0,0 +1,15 @@
|
||||
# Copyright (c) 2021 Dominic Msters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
add_subdirectory(audiogen)
|
||||
|
||||
# Texture Tool
|
||||
function(tool_audio target in)
|
||||
add_custom_target(${target}
|
||||
COMMAND audiogen "${DAWN_ASSETS_SOURCE_DIR}/${in}" "${DAWN_ASSETS_BUILD_DIR}/${target}"
|
||||
COMMENT "Generating audio ${target} from ${in}"
|
||||
DEPENDS audiogen
|
||||
)
|
||||
endfunction()
|
23
src/dawntools/audio/audiogen/CMakeLists.txt
Normal file
23
src/dawntools/audio/audiogen/CMakeLists.txt
Normal file
@ -0,0 +1,23 @@
|
||||
# Copyright (c) 2021 Dominic Msters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Texture Build Tool
|
||||
project(audiogen VERSION 1.0)
|
||||
add_executable(audiogen)
|
||||
target_sources(audiogen
|
||||
PRIVATE
|
||||
main.cpp
|
||||
../../utils/file.c
|
||||
)
|
||||
target_include_directories(audiogen
|
||||
PUBLIC
|
||||
${CMAKE_CURRENT_LIST_DIR}/../../
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
)
|
||||
target_link_libraries(audiogen
|
||||
PUBLIC
|
||||
${DAWN_BUILD_HOST_LIBS}
|
||||
AudioFile
|
||||
)
|
78
src/dawntools/audio/audiogen/main.cpp
Normal file
78
src/dawntools/audio/audiogen/main.cpp
Normal file
@ -0,0 +1,78 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
extern "C" {
|
||||
#include "../../utils/common.h"
|
||||
#include "../../utils/file.h"
|
||||
}
|
||||
#include "AudioFile.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
FILE *fileOut;
|
||||
char buffer[FILENAME_MAX];
|
||||
size_t bufferLength = 0;
|
||||
|
||||
if(argc != 3) {
|
||||
printf("Invalid number of arguments\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
fileNormalizeSlashes(argv[1]);
|
||||
fileNormalizeSlashes(argv[2]);
|
||||
std::string strFileIn = std::string(argv[1]);
|
||||
std::string strFileOut = std::string(argv[2]);
|
||||
|
||||
buffer[0] = '\0';
|
||||
sprintf(buffer, "%s.audio", strFileOut.c_str());
|
||||
fileOut = fopen(buffer, "rb");
|
||||
if(fileOut != NULL) {
|
||||
fclose(fileOut);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Load input file
|
||||
AudioFile<double_t> audioFile;
|
||||
if(!audioFile.load(strFileIn)) {
|
||||
printf("Failed to load audio file.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Open Output File
|
||||
fileMkdirp(buffer);
|
||||
fileOut = fopen(buffer, "wb");
|
||||
if(fileOut == NULL) {
|
||||
printf("Failed to create output file\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Write header
|
||||
buffer[0] = '\0';
|
||||
sprintf(buffer, "%i|%i|%i|%i|",
|
||||
audioFile.getNumChannels(),
|
||||
audioFile.getSampleRate(),
|
||||
audioFile.getNumSamplesPerChannel(),
|
||||
audioFile.getNumSamplesPerChannel() * audioFile.getNumChannels()*(
|
||||
sizeof(int16_t) / sizeof(uint8_t)
|
||||
)
|
||||
);
|
||||
bufferLength = strlen(buffer);
|
||||
fwrite(buffer, sizeof(char), bufferLength, fileOut);
|
||||
|
||||
// Convert Data to 16 bit audio
|
||||
for (int i = 0; i < audioFile.getNumSamplesPerChannel(); i++) {
|
||||
for(int y = 0; y < audioFile.getNumChannels(); y++) {
|
||||
double_t sample = audioFile.samples[y][i];
|
||||
sample = sample < -1 ? -1 : sample > 1 ? 1 : sample;
|
||||
auto q = static_cast<int16_t> (sample * 32767.);
|
||||
|
||||
buffer[0] = (q >> 8) & 0xFF;
|
||||
buffer[1] = q & 0xFF;
|
||||
fwrite(buffer, sizeof(uint8_t), 2, fileOut);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fileOut);
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user