… | |
… | |
161 | our %SDL_CB; |
161 | our %SDL_CB; |
162 | |
162 | |
163 | our $SDL_MIXER; |
163 | our $SDL_MIXER; |
164 | our $MUSIC_DEFAULT = "in_a_heartbeat.ogg"; |
164 | our $MUSIC_DEFAULT = "in_a_heartbeat.ogg"; |
165 | our @MUSIC_WANT; |
165 | our @MUSIC_WANT; |
|
|
166 | our $MUSIC_START; |
166 | our $MUSIC_PLAYING; |
167 | our $MUSIC_PLAYING; |
167 | our $MUSIC_PLAYER; |
168 | our $MUSIC_PLAYER; |
|
|
169 | our $MUSIC_RESUME = 30; # resume music when players less than these many seconds before |
168 | our @SOUNDS; # event => file mapping |
170 | our @SOUNDS; # event => file mapping |
169 | our %AUDIO_CHUNKS; # audio files |
171 | our %AUDIO_CHUNKS; # audio files |
170 | |
172 | |
171 | our $ALT_ENTER_MESSAGE; |
173 | our $ALT_ENTER_MESSAGE; |
172 | our $STATUSBOX; |
174 | our $STATUSBOX; |
… | |
… | |
501 | $SETUP_NOTEBOOK->set_current_page ($SETUP_SERVER); |
503 | $SETUP_NOTEBOOK->set_current_page ($SETUP_SERVER); |
502 | $SETUP_DIALOG->show; |
504 | $SETUP_DIALOG->show; |
503 | $PL_WINDOW->hide; |
505 | $PL_WINDOW->hide; |
504 | $SPELL_LIST->clear_spells; |
506 | $SPELL_LIST->clear_spells; |
505 | $CFPlus::UI::ROOT->emit (stop_game => ! ! $CONN); |
507 | $CFPlus::UI::ROOT->emit (stop_game => ! ! $CONN); |
|
|
508 | |
|
|
509 | &audio_music_set ([]); |
506 | |
510 | |
507 | return unless $CONN; |
511 | return unless $CONN; |
508 | |
512 | |
509 | status "connection closed"; |
513 | status "connection closed"; |
510 | |
514 | |
… | |
… | |
2015 | } |
2019 | } |
2016 | |
2020 | |
2017 | sub audio_music_set { |
2021 | sub audio_music_set { |
2018 | my ($songs) = @_; |
2022 | my ($songs) = @_; |
2019 | |
2023 | |
2020 | my $count = @$songs; |
2024 | my @want = |
2021 | my @meta; |
2025 | grep $_, |
|
|
2026 | map $CONN->{music_meta}{$_}, |
|
|
2027 | @$songs; |
2022 | |
2028 | |
2023 | for my $name (@$songs) { |
2029 | if (@want) { |
2024 | CFPlus::DB::get resmap => $name, sub { |
|
|
2025 | my ($data) = @_; |
|
|
2026 | |
|
|
2027 | if ($data) { |
|
|
2028 | $data = JSON::XS->new->utf8->decode ($data); |
|
|
2029 | $data->{path} = CFPlus::DB::path_of $name; |
|
|
2030 | push @meta, $data; |
|
|
2031 | } |
|
|
2032 | |
|
|
2033 | unless (--$count) { |
|
|
2034 | if (@meta) { |
|
|
2035 | @MUSIC_WANT = @meta; |
2030 | @MUSIC_WANT = @want; |
2036 | &audio_music_changed (); |
2031 | &audio_music_changed (); |
2037 | } |
2032 | } |
2038 | } |
2033 | } |
|
|
2034 | |
|
|
2035 | sub audio_music_start { |
|
|
2036 | my $path = $MUSIC_PLAYING->{path} |
|
|
2037 | or return; |
|
|
2038 | |
|
|
2039 | CFPlus::DB::prefetch_file $path, 1024_000, sub { |
|
|
2040 | # music might have changed... |
|
|
2041 | $path eq $MUSIC_PLAYING->{path} |
|
|
2042 | or return &audio_music_start (); |
|
|
2043 | |
|
|
2044 | $MUSIC_PLAYER = new_from_file CFPlus::MixMusic $path; |
|
|
2045 | |
|
|
2046 | my $NOW = time; |
|
|
2047 | |
|
|
2048 | if ($MUSIC_PLAYING->{stop_time} > $NOW - $MUSIC_RESUME) { |
|
|
2049 | my $pos = $MUSIC_PLAYING->{stop_pos}; |
|
|
2050 | $MUSIC_PLAYER->fade_in_pos (0, 1000, $pos); |
|
|
2051 | $MUSIC_START = time - $pos; |
|
|
2052 | } else { |
|
|
2053 | $MUSIC_PLAYER->play (0); |
|
|
2054 | $MUSIC_START = time; |
2039 | }; |
2055 | } |
|
|
2056 | |
|
|
2057 | delete $MUSIC_PLAYING->{stop_time}; |
|
|
2058 | delete $MUSIC_PLAYING->{stop_pos}; |
2040 | } |
2059 | } |
2041 | } |
2060 | } |
2042 | |
2061 | |
2043 | sub audio_music_changed { |
2062 | sub audio_music_changed { |
2044 | return unless $CFG->{bgm_enable}; |
2063 | return unless $CFG->{bgm_enable}; |
2045 | |
2064 | |
2046 | # default MUSIC_WANT, undef/0 == MUSIC_DEFAULT |
2065 | # default MUSIC_WANT == MUSIC_DEFAULT |
2047 | @MUSIC_WANT = { path => CFPlus::find_rcfile "music/$MUSIC_DEFAULT" } unless @MUSIC_WANT; |
2066 | @MUSIC_WANT = { path => CFPlus::find_rcfile "music/$MUSIC_DEFAULT" } unless @MUSIC_WANT; |
2048 | |
2067 | |
2049 | # if the currently playing song is acceptable, let it continue |
2068 | # if the currently playing song is acceptable, let it continue |
|
|
2069 | return if $MUSIC_PLAYING |
2050 | return if grep $MUSIC_PLAYING eq $_->{path}, @MUSIC_WANT; |
2070 | && grep $MUSIC_PLAYING->{path} eq $_->{path}, @MUSIC_WANT; |
2051 | |
2071 | |
|
|
2072 | my $NOW = time; |
|
|
2073 | |
2052 | if (defined $MUSIC_PLAYING) { |
2074 | if ($MUSIC_PLAYING) { |
2053 | warn "fade out"; |
2075 | $MUSIC_PLAYING->{stop_time} = $NOW; |
|
|
2076 | $MUSIC_PLAYING->{stop_pos} = $NOW - $MUSIC_START; |
2054 | CFPlus::MixMusic::fade_out 2000; |
2077 | CFPlus::MixMusic::fade_out 1000; |
2055 | } else { |
2078 | } else { |
2056 | warn "new music\n"; |
2079 | # sort by stop time, oldest first |
|
|
2080 | @MUSIC_WANT = sort { $a->{stop_time} <=> $b->{stop_time} } @MUSIC_WANT; |
2057 | |
2081 | |
2058 | my $music = $MUSIC_WANT [rand @MUSIC_WANT]; |
2082 | # if the most recently-played piece played very recently, |
|
|
2083 | # resume it, else choose the oldest piece for rotation. |
|
|
2084 | $MUSIC_PLAYING = |
|
|
2085 | $MUSIC_WANT[-1]{stop_time} > $NOW - $MUSIC_RESUME |
|
|
2086 | ? $MUSIC_WANT[-1] |
|
|
2087 | : $MUSIC_WANT[0]; |
2059 | |
2088 | |
2060 | $MUSIC_PLAYING = $music->{path}; |
2089 | audio_music_start; |
2061 | |
|
|
2062 | $MUSIC_PLAYER = new_from_file CFPlus::MixMusic $music->{path}; |
|
|
2063 | $MUSIC_PLAYER->play (0); |
|
|
2064 | } |
2090 | } |
2065 | } |
2091 | } |
2066 | |
2092 | |
2067 | sub audio_music_finished { |
2093 | sub audio_music_finished { |
2068 | $MUSIC_PLAYING = undef; |
2094 | $MUSIC_PLAYING = undef; |