Fixed crushing midi outputs

This commit is contained in:
Robert Bendun 2022-11-23 21:51:27 +01:00
parent 07fa4f894a
commit 25cf883d03
3 changed files with 10 additions and 6 deletions

View File

@ -605,7 +605,6 @@ static Result<Value> builtin_scan(Interpreter &interpreter, std::vector<Value> a
static Result<Value> builtin_concurrent(Interpreter &interpreter, std::span<Ast> args) static Result<Value> builtin_concurrent(Interpreter &interpreter, std::span<Ast> args)
{ {
auto const jobs_count = args.size(); auto const jobs_count = args.size();
std::vector<std::jthread> threads;
std::vector<std::future<Value>> futures; std::vector<std::future<Value>> futures;
std::optional<Error> error; std::optional<Error> error;
std::mutex mutex; std::mutex mutex;
@ -613,7 +612,7 @@ static Result<Value> builtin_concurrent(Interpreter &interpreter, std::span<Ast>
for (unsigned i = 0; i < jobs_count; ++i) { for (unsigned i = 0; i < jobs_count; ++i) {
futures.push_back(std::async(std::launch::async, [interpreter = interpreter.clone(), i, args, &mutex, &error]() mutable -> Value { futures.push_back(std::async(std::launch::async, [interpreter = interpreter.clone(), i, args, &mutex, &error]() mutable -> Value {
auto result = interpreter.eval((Ast)args[i]); auto result = interpreter.eval((Ast)args[i]);
if (result.has_value()) { if (result) {
return *std::move(result); return *std::move(result);
} }
@ -621,7 +620,7 @@ static Result<Value> builtin_concurrent(Interpreter &interpreter, std::span<Ast>
if (!error) { if (!error) {
error = result.error(); error = result.error();
} }
return Value{}; return Value{};
})); }));
} }

View File

@ -6,6 +6,7 @@
#include <thread> #include <thread>
#include <cstring> #include <cstring>
#include <cstdio> #include <cstdio>
#include <mutex>
#include <musique/format.hh> #include <musique/format.hh>
#include <musique/interpreter/env.hh> #include <musique/interpreter/env.hh>
@ -188,7 +189,9 @@ struct Runner
} }
} }
Env::global->force_define("say", +[](Interpreter &interpreter, std::vector<Value> args) -> Result<Value> { Env::global->force_define("print", +[](Interpreter &interpreter, std::vector<Value> args) -> Result<Value> {
static std::mutex stdio_mutex;
std::lock_guard guard{stdio_mutex};
for (auto it = args.begin(); it != args.end(); ++it) { for (auto it = args.begin(); it != args.end(); ++it) {
std::cout << Try(format(interpreter, *it)); std::cout << Try(format(interpreter, *it));
if (std::next(it) != args.end()) if (std::next(it) != args.end())

View File

@ -1,5 +1,6 @@
#include <musique/midi/midi.hh> #include <musique/midi/midi.hh>
#include <musique/errors.hh> #include <musique/errors.hh>
#include <mutex>
// Copyright notice for RtMidi library // Copyright notice for RtMidi library
__asm__(R"license(.ident "\ __asm__(R"license(.ident "\
@ -7,6 +8,8 @@ RtMidi: realtime MIDI i/o C++ classes\
Copyright (c) 2003-2021 Gary P. Scavone" Copyright (c) 2003-2021 Gary P. Scavone"
)license"); )license");
static std::mutex midi_output_mutex;
void midi::Rt_Midi::list_ports(std::ostream &out) const void midi::Rt_Midi::list_ports(std::ostream &out) const
try { try {
RtMidiIn input; RtMidiIn input;
@ -80,6 +83,7 @@ bool midi::Rt_Midi::supports_output() const
template<std::size_t N> template<std::size_t N>
inline void send_message(RtMidiOut &out, std::array<std::uint8_t, N> message) inline void send_message(RtMidiOut &out, std::array<std::uint8_t, N> message)
try { try {
std::lock_guard guard{midi_output_mutex};
out.sendMessage(message.data(), message.size()); out.sendMessage(message.data(), message.size());
} catch (RtMidiError &error) { } catch (RtMidiError &error) {
// TODO(error) // TODO(error)
@ -97,13 +101,11 @@ enum : std::uint8_t
void midi::Rt_Midi::send_note_on(uint8_t channel, uint8_t note_number, uint8_t velocity) void midi::Rt_Midi::send_note_on(uint8_t channel, uint8_t note_number, uint8_t velocity)
{ {
std::cout << "NOTE ON " << int(channel) << '\t' << int(note_number) << '\t' << int(velocity) << std::endl;
send_message(*output, std::array { std::uint8_t(Note_On + channel), note_number, velocity }); send_message(*output, std::array { std::uint8_t(Note_On + channel), note_number, velocity });
} }
void midi::Rt_Midi::send_note_off(uint8_t channel, uint8_t note_number, uint8_t velocity) void midi::Rt_Midi::send_note_off(uint8_t channel, uint8_t note_number, uint8_t velocity)
{ {
std::cout << "NOTE OFF " << int(channel) << '\t' << int(note_number) << '\t' << int(velocity) << std::endl;
send_message(*output, std::array { std::uint8_t(Note_Off + channel), note_number, velocity }); send_message(*output, std::array { std::uint8_t(Note_Off + channel), note_number, velocity });
} }