Skip to content

Commit

Permalink
fix: compatibility with GCC <10 (#74)
Browse files Browse the repository at this point in the history
Co-authored-by: Aaron <[email protected]>
  • Loading branch information
mmyjona and aarnphm committed Mar 22, 2023
1 parent 44d9101 commit a38d8d9
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions src/whispercpp/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
#include <pybind11/pytypes.h>
#endif

#if __GNUC__ > 10 || defined(__clang__)
#define STREAM_CAST
#else
#define STREAM_CAST static_cast<std::stringstream &>
#endif

#define NO_STATE_WARNING(no_state) \
do { \
if (no_state) { \
Expand All @@ -17,6 +23,7 @@
} \
} while (0)

#if __GNUC__ > 10 || defined(__clang__)
#define RAISE_RUNTIME_ERROR(msg) \
do { \
throw std::runtime_error((std::stringstream() \
Expand All @@ -25,6 +32,17 @@
<< "\n") \
.str()); \
} while (0)
#else
#define RAISE_RUNTIME_ERROR(msg) \
do { \
throw std::runtime_error( \
static_cast<std::stringstream &>(std::stringstream() \
<< __FILE__ << "#L" \
<< std::to_string(__LINE__) \
<< ": " << msg << "\n") \
.str()); \
} while (0)
#endif

#define RAISE_IF_NULL(ptr) \
do { \
Expand Down Expand Up @@ -257,14 +275,14 @@ std::vector<float> Context::lang_detect(size_t offset_ms, size_t threads) {
}

if (res == -1) {
RAISE_RUNTIME_ERROR((std::stringstream()
<< "offset " << offset_ms
<< "ms is before the start of audio.")
RAISE_RUNTIME_ERROR(STREAM_CAST(std::stringstream()
<< "offset " << offset_ms
<< "ms is before the start of audio.")
.str());
} else if (res == -2) {
RAISE_RUNTIME_ERROR((std::stringstream()
<< "offset " << offset_ms
<< "ms is past the end of the audio.")
RAISE_RUNTIME_ERROR(STREAM_CAST(std::stringstream()
<< "offset " << offset_ms
<< "ms is past the end of the audio.")
.str());
} else if (res == -6) {
RAISE_RUNTIME_ERROR("Failed to encode.");
Expand Down Expand Up @@ -372,11 +390,12 @@ int Context::full(Params params, std::vector<float> data) {
} else if (ret == -3) {
RAISE_RUNTIME_ERROR("Failed to auto-detect language.");
} else if (ret == -5) {
RAISE_RUNTIME_ERROR((std::stringstream()
<< "audio_ctx is larger than maximum allowed ("
<< std::to_string(params.get()->audio_ctx) << " > "
<< this->n_audio_ctx() << ").")
.str());
RAISE_RUNTIME_ERROR(
STREAM_CAST(std::stringstream()
<< "audio_ctx is larger than maximum allowed ("
<< std::to_string(params.get()->audio_ctx) << " > "
<< this->n_audio_ctx() << ").")
.str());
} else if (ret == -6) {
RAISE_RUNTIME_ERROR("Failed to encode.");
} else if (ret == -7 || ret == -8) {
Expand Down Expand Up @@ -418,11 +437,12 @@ int Context::full_parallel(Params params, std::vector<float> data,
} else if (ret == -3) {
RAISE_RUNTIME_ERROR("Failed to auto-detect language.");
} else if (ret == -5) {
RAISE_RUNTIME_ERROR((std::stringstream()
<< "audio_ctx is larger than maximum allowed ("
<< std::to_string(params.get()->audio_ctx) << " > "
<< this->n_audio_ctx() << ").")
.str());
RAISE_RUNTIME_ERROR(
STREAM_CAST(std::stringstream()
<< "audio_ctx is larger than maximum allowed ("
<< std::to_string(params.get()->audio_ctx) << " > "
<< this->n_audio_ctx() << ").")
.str());
} else if (ret == -6) {
RAISE_RUNTIME_ERROR("Failed to encode.");
} else if (ret == -7 || ret == -8) {
Expand Down

0 comments on commit a38d8d9

Please sign in to comment.