1
Fork 0

Lines are now communicated to server, and pitches are correct :D

This commit is contained in:
Joshua Moerman 2013-04-17 10:28:31 +02:00
parent ffa44c81f0
commit 5a2eef7a7e
3 changed files with 23 additions and 14 deletions

View file

@ -3,6 +3,9 @@
#include <cmath> #include <cmath>
#include <stdexcept> #include <stdexcept>
#include <lib/libwebsockets.h>
#include "../../../beats-server/src/websockets.h"
#include "../../../beats-server/src/json.h" #include "../../../beats-server/src/json.h"
#include "../../../beats-server/src/json_ext.h" #include "../../../beats-server/src/json_ext.h"
@ -18,7 +21,12 @@ namespace games {
libwebsocket_callback_on_writable(context, wsi); libwebsocket_callback_on_writable(context, wsi);
break; break;
case LWS_CALLBACK_CLIENT_RECEIVE: case LWS_CALLBACK_CLIENT_RECEIVE:
// std::cout << std::string(static_cast<char*>(in), len) << std::endl; try {
auto l = from_json<cheap_line_type>(parse_json(std::string((char*)in, len)));
current_client->add_line(l);
} catch(std::exception& e){
throw websockets::runtime_error(e.what());
}
break; break;
case LWS_CALLBACK_CLIENT_WRITEABLE:{ case LWS_CALLBACK_CLIENT_WRITEABLE:{
js::Object ret; js::Object ret;
@ -47,6 +55,13 @@ namespace games {
{ NULL, NULL, 0 } { NULL, NULL, 0 }
}; };
void update_pitches(Client& client){
for(auto & l : client.lines){
auto& sound = l->sound;
sound->set_pitch(pitch_for_midi_note(client.scale.note_for_length(std::sqrt(l->sqr_length()))));
}
}
void Client::add_line(cheap_line_type const & line_in){ void Client::add_line(cheap_line_type const & line_in){
auto line = create_instrument(line_in); auto line = create_instrument(line_in);
line->sound.reset(new motor::al::Source(sounds[line->line_kind])); line->sound.reset(new motor::al::Source(sounds[line->line_kind]));
@ -57,19 +72,13 @@ namespace games {
} }
lines_changed = true; lines_changed = true;
update_pitches(*this);
} }
bool Client::has_ended(){ bool Client::has_ended(){
return false; return false;
} }
void update_pitches(Client& client){
for(auto & l : client.lines){
auto& sound = l->sound;
sound->set_pitch(pitch_for_midi_note(client.scale.note_for_length(std::sqrt(l->sqr_length()))));
}
}
Client::Client(int window_width_, int window_height_, std::shared_ptr<Base>& active_base_) Client::Client(int window_width_, int window_height_, std::shared_ptr<Base>& active_base_)
: Base(window_width_, window_height_, active_base_) : Base(window_width_, window_height_, active_base_)
{ {
@ -78,15 +87,14 @@ namespace games {
} }
current_client = this; current_client = this;
// sound
scale = Scale::load_from_file(*current_scale);
// simulation // simulation
beat.notes.emplace_back(note_type::kQuarterNote, note_info{100.0f, 100.0f}); beat.notes.emplace_back(note_type::kQuarterNote, note_info{100.0f, 100.0f});
cheap_line_type line{{{50.0f, 100.0f}, {150.0f, 200.0f}, simulation::kOneWay}}; cheap_line_type line{{{50.0f, 100.0f}, {150.0f, 200.0f}, simulation::kOneWay}};
add_line(line); add_line(line);
// sound
scale = Scale::load_from_file(*current_scale);
update_pitches(*this);
// websockets // websockets
lws_context_creation_info info; lws_context_creation_info info;
memset(&info, 0, sizeof info); memset(&info, 0, sizeof info);

View file

@ -40,8 +40,8 @@ namespace games {
::bundle.get_sound_path() + "pentatonic" ::bundle.get_sound_path() + "pentatonic"
}; };
const std::vector<std::shared_ptr<motor::al::Buffer>> sounds{ const std::vector<std::shared_ptr<motor::al::Buffer>> sounds{
resource_cache.get_sound_buffer(::bundle.get_sound_path() + "marimba.wav"), resource_cache.get_sound_buffer(::bundle.get_sound_path() + "guitar440.wav"),
resource_cache.get_sound_buffer(::bundle.get_sound_path() + "guitar440.wav") resource_cache.get_sound_buffer(::bundle.get_sound_path() + "marimba.wav")
}; };
std::vector<std::string>::const_iterator current_scale{scales.begin()}; std::vector<std::string>::const_iterator current_scale{scales.begin()};
Scale scale; Scale scale;

View file

@ -103,6 +103,7 @@ struct cheap_line_type {
IntVec2 end_point; IntVec2 end_point;
int line_kind; int line_kind;
cheap_line_type() = default;
cheap_line_type(Instrument const & l) cheap_line_type(Instrument const & l)
: starting_point(l.starting_point) : starting_point(l.starting_point)
, end_point(l.end_point) , end_point(l.end_point)