ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/cfplus
(Generate patch)

Comparing deliantra/Deliantra-Client/bin/cfplus (file contents):
Revision 1.167 by root, Fri Jul 13 05:31:32 2007 UTC vs.
Revision 1.168 by root, Sat Jul 14 12:05:53 2007 UTC

161our %SDL_CB; 161our %SDL_CB;
162 162
163our $SDL_MIXER; 163our $SDL_MIXER;
164our $MUSIC_DEFAULT = "in_a_heartbeat.ogg"; 164our $MUSIC_DEFAULT = "in_a_heartbeat.ogg";
165our @MUSIC_WANT; 165our @MUSIC_WANT;
166our $MUSIC_START;
166our $MUSIC_PLAYING; 167our $MUSIC_PLAYING;
167our $MUSIC_PLAYER; 168our $MUSIC_PLAYER;
169our $MUSIC_RESUME = 30; # resume music when players less than these many seconds before
168our @SOUNDS; # event => file mapping 170our @SOUNDS; # event => file mapping
169our %AUDIO_CHUNKS; # audio files 171our %AUDIO_CHUNKS; # audio files
170 172
171our $ALT_ENTER_MESSAGE; 173our $ALT_ENTER_MESSAGE;
172our $STATUSBOX; 174our $STATUSBOX;
2017} 2019}
2018 2020
2019sub audio_music_set { 2021sub audio_music_set {
2020 my ($songs) = @_; 2022 my ($songs) = @_;
2021 2023
2022 my $count = @$songs; 2024 my @want =
2023 my @meta; 2025 grep $_,
2026 map $CONN->{music_meta}{$_},
2027 @$songs;
2024 2028
2025 for my $name (@$songs) { 2029 if (@want) {
2026 CFPlus::DB::get "res-metadata" => $name, sub {
2027 my ($data) = @_;
2028
2029 if ($data) {
2030 $data = JSON::XS->new->utf8->decode ($data);
2031 $data->{path} = CFPlus::DB::path_of $name;
2032 push @meta, $data;
2033 }
2034
2035 unless (--$count) {
2036 if (@meta) {
2037 @MUSIC_WANT = @meta; 2030 @MUSIC_WANT = @want;
2038 &audio_music_changed (); 2031 &audio_music_changed ();
2039 } 2032 }
2040 } 2033}
2034
2035sub 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;
2041 }; 2055 }
2056
2057 delete $MUSIC_PLAYING->{stop_time};
2058 delete $MUSIC_PLAYING->{stop_pos};
2042 } 2059 }
2043} 2060}
2044 2061
2045sub audio_music_changed { 2062sub audio_music_changed {
2046 return unless $CFG->{bgm_enable}; 2063 return unless $CFG->{bgm_enable};
2050 2067
2051 # if the currently playing song is acceptable, let it continue 2068 # if the currently playing song is acceptable, let it continue
2052 return if $MUSIC_PLAYING 2069 return if $MUSIC_PLAYING
2053 && grep $MUSIC_PLAYING->{path} eq $_->{path}, @MUSIC_WANT; 2070 && grep $MUSIC_PLAYING->{path} eq $_->{path}, @MUSIC_WANT;
2054 2071
2072 my $NOW = time;
2073
2055 if ($MUSIC_PLAYING) { 2074 if ($MUSIC_PLAYING) {
2075 $MUSIC_PLAYING->{stop_time} = $NOW;
2076 $MUSIC_PLAYING->{stop_pos} = $NOW - $MUSIC_START;
2056 CFPlus::MixMusic::fade_out 1000; 2077 CFPlus::MixMusic::fade_out 1000;
2057 } else { 2078 } else {
2058 my $music = $MUSIC_WANT [rand @MUSIC_WANT]; 2079 # sort by stop time, oldest first
2080 @MUSIC_WANT = sort { $a->{stop_time} <=> $b->{stop_time} } @MUSIC_WANT;
2059 2081
2082 # if the most recently-played piece played very recently,
2083 # resume it, else choose the oldest piece for rotation.
2060 $MUSIC_PLAYING = $music; 2084 $MUSIC_PLAYING =
2085 $MUSIC_WANT[-1]{stop_time} > $NOW - $MUSIC_RESUME
2086 ? $MUSIC_WANT[-1]
2087 : $MUSIC_WANT[0];
2061 2088
2062 $MUSIC_PLAYER = new_from_file CFPlus::MixMusic $music->{path}; 2089 audio_music_start;
2063 $MUSIC_PLAYER->play (0);
2064 } 2090 }
2065} 2091}
2066 2092
2067sub audio_music_finished { 2093sub audio_music_finished {
2068 $MUSIC_PLAYING = undef; 2094 $MUSIC_PLAYING = undef;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines