Skip to content

Commit

Permalink
Refactorized main.cpp, separated the audio player related procedures …
Browse files Browse the repository at this point in the history
…into another class; no longer writing in non-reserved memory which was causing runtime error when compiled with MSVC
  • Loading branch information
FaintWhisper committed Dec 28, 2021
1 parent ccc90b0 commit a3fdd78
Show file tree
Hide file tree
Showing 15 changed files with 546 additions and 211 deletions.
63 changes: 63 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto

###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp

###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary

###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary

###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.idea
cmake-build-debug
.vs
out
4 changes: 0 additions & 4 deletions .idea/misc.xml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

2 changes: 0 additions & 2 deletions .idea/wasabi.iml

This file was deleted.

16 changes: 11 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@ project(wasabi)

set(CMAKE_CXX_STANDARD 14)

set(WAV_FORMAT_READER audio_format_readers/wav)
set(WASAPI audio_protocols/wasapi)
set(AUDIO_FORMAT_READERS audio_format_readers)
set(WAV_FORMAT_READER ${AUDIO_FORMAT_READERS}/wav)
set(AUDIO_PROTOCOLS audio_protocols)
set(WASAPI ${AUDIO_PROTOCOLS}/wasapi)
set(PLAYER player)

#Include the directories and now your cpp files will recognize your headers
include_directories(${WAV_FORMAT_READER})
include_directories(${WASAPI})
include_directories(${PLAYER})

set(
SOURCE_FILES
main.cpp
${WAV_FORMAT_READER}/wav_reader.hpp
${WAV_FORMAT_READER}/wav_reader.cpp
${PLAYER}/player.hpp
${PLAYER}/player.cpp
${WASAPI}/wasapi.hpp
${WASAPI}/wasapi.cpp)
${WASAPI}/wasapi.cpp
"wasabi.cpp"
)

add_executable(wasabi ${SOURCE_FILES})
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "wasabi")
15 changes: 15 additions & 0 deletions CMakeSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"configurations": [
{
"name": "x64-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"inheritEnvironments": [ "msvc_x64_x64" ],
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": ""
}
]
}
40 changes: 21 additions & 19 deletions audio_format_readers/wav/wav_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void WAVReader::load_file(std::string *file_path) {
std::shared_ptr<std::ifstream> file = std::make_shared<std::ifstream>(this->audio_file_path,
std::ios::in | std::ios::binary);

if (file != nullptr) {
if (file != nullptr && file->is_open()) {
std::cout << "\n[Loaded \"" << *file_path << "\"]" << std::flush;

check_riff_header(file);
Expand All @@ -36,18 +36,18 @@ void WAVReader::load_file(std::string *file_path) {
};

void WAVReader::check_riff_header(std::shared_ptr<std::ifstream> file) {
char file_chunk_id[4];
char file_chunk_id[5];
uint32_t file_chunk_size;
char file_format[4];
char file_format[5];

std::cout << "\nChecking the file header..." << std::endl;

// Checks the chunk ID
file->read(file_chunk_id, sizeof(file_chunk_id));
file->read(file_chunk_id, sizeof(file_chunk_id) - 1);

file_chunk_id[sizeof(file_chunk_id)] = '\0';
file_chunk_id[sizeof(file_chunk_id) - 1] = '\0';

if (strcmp(file_chunk_id, "RIFF") == 0) {
if (strncmp(file_chunk_id, "RIFF", sizeof(file_chunk_id) - 1) == 0) {
std::cout << "Chunk ID: Ok." << std::endl;

strcpy(this->chunk_id, file_chunk_id);
Expand All @@ -71,11 +71,11 @@ void WAVReader::check_riff_header(std::shared_ptr<std::ifstream> file) {
}

// Checks the format descriptor
file->read(file_format, sizeof(file_format));
file->read(file_format, sizeof(file_format) - 1);

file_format[sizeof(file_format)] = '\0';
file_chunk_id[sizeof(file_chunk_id) - 1] = '\0';

if (strcmp(file_format, "WAVE") == 0) {
if (strncmp(file_format, "WAVE", sizeof(file_format) - 1) == 0) {
std::cout << "Format descriptor: Ok." << std::endl;

strcpy(this->format_descriptor, file_format);
Expand All @@ -84,12 +84,10 @@ void WAVReader::check_riff_header(std::shared_ptr<std::ifstream> file) {

exit(EXIT_FAILURE);
}

file->tellg();
};

void WAVReader::load_fmt_subchunk(std::shared_ptr<std::ifstream> file) {
char file_fmt_id[4];
char file_fmt_id[5];
uint32_t file_fmt_size;
uint16_t file_fmt_audio_format;
uint16_t file_fmt_num_channels;
Expand All @@ -101,9 +99,11 @@ void WAVReader::load_fmt_subchunk(std::shared_ptr<std::ifstream> file) {
std::cout << "\nReading the 'fmt' subchunk..." << std::endl;

// Checks the fmt subchunk ID
file->read(file_fmt_id, sizeof(file_fmt_id));
file->read(file_fmt_id, sizeof(file_fmt_id) - 1);

file_fmt_id[sizeof(file_fmt_id) - 1] = '\0';

if (strncmp(file_fmt_id, "fmt", sizeof(file_fmt_id) - 1) == 0) {
if (strncmp(file_fmt_id, "fmt", sizeof(file_fmt_id) - 2) == 0) {
std::cout << "fmt subchunk ID: Ok." << std::endl;

strcpy(this->chunk_id, file_fmt_id);
Expand All @@ -114,7 +114,7 @@ void WAVReader::load_fmt_subchunk(std::shared_ptr<std::ifstream> file) {
}

// Gets the fmt subchunk size and checks the audio encoding
file->read(reinterpret_cast<char *> (&file_fmt_size), sizeof(file_fmt_size));
file->read((char*) &file_fmt_size, sizeof(file_fmt_size));

if (file_fmt_size == 16) {
std::cout << "fmt subchunk size: Ok (" << file_fmt_size << " bytes)." << std::endl;
Expand Down Expand Up @@ -217,13 +217,15 @@ void WAVReader::load_fmt_subchunk(std::shared_ptr<std::ifstream> file) {
};

void WAVReader::load_data_subchunk(std::shared_ptr<std::ifstream> file) {
char file_data_id[4];
char file_data_id[5];
uint32_t file_data_size;

std::cout << "\nReading the 'data' subchunk..." << std::endl;

// Checks the data sbuchunk ID
file->read(file_data_id, sizeof(file_data_id));
file->read(file_data_id, sizeof(file_data_id) - 1);

file_data_id[sizeof(file_data_id) - 1] = '\0';

if (strncmp(file_data_id, "data", sizeof(file_data_id)) == 0) {
std::cout << "data subchunk ID: Ok." << std::endl;
Expand Down Expand Up @@ -274,7 +276,7 @@ void WAVReader::load_data(std::shared_ptr<std::ifstream> file) {

if (!this->is_playback_started && (current_file_chunk == (MAX_AUDIO_BUFFER_CHUNKS - 1) || this->audio_buffer_chunks[current_file_chunk].is_eof)) {
this->is_audio_buffer_ready = TRUE;
this->cv->notify_one();
this->cv.notify_one();
}

current_file_chunk += 1;
Expand All @@ -295,7 +297,7 @@ bool WAVReader::get_chunk(BYTE **chunk, uint32_t &chunk_size) {
std::unique_lock<std::mutex> lck(this->mtx);

while (!this->is_audio_buffer_ready) {
this->cv->wait(lck);
this->cv.wait(lck);
}

while (this->audio_buffer_chunks[this->current_audio_buffer_chunk].data == nullptr) {
Expand Down
2 changes: 1 addition & 1 deletion audio_format_readers/wav/wav_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class WAVReader {
int current_audio_buffer_chunk{};
bool is_audio_buffer_ready{};
bool is_playback_started{};
std::condition_variable *cv{};
std::condition_variable cv;
std::mutex mtx;

void check_riff_header(std::shared_ptr<std::ifstream> file);
Expand Down
Loading

0 comments on commit a3fdd78

Please sign in to comment.