Audio-Daemon

 view release on metacpan or  search on metacpan

Daemon/MPG123.pm  view on Meta::CPAN

  $player->stop;
}
  
sub play {
  my $self = shift;
  my $remote = shift;
  my $player = $self->player;
  if (defined $remote && ref $remote && scalar @{$remote->{args}}) {
    if ($self->{state}{random}) {
      $self->{state}{renid} = $self->{revrandom}[$remote->{args}[0]];
      $self->{state}{regid} = $remote->{args}[0];
    }
  } elsif ($player->state == 1) {
    $player->pause; # toggles pause flag (starts playing)
    return;
  }
  $self->debug("Random is ".($self->{state}{random})?'true':'false');
  my $id = $self->{state}{regid};
  $self->info("Setting current playlist id to $id and song to ".$self->{playlist}[$id]);
  if (! -r $self->{playlist}[$id] && ($self->{playlist}[$id]!~/^http/)) {
    $self->crit("File Not Found or Not Readable: ".$self->{playlist}[$id]);
    return;
  }
  my $retval = eval { $player->load($self->{playlist}[$id]); };
  $self->crit($@) if ($@);
  $self->info("load returned $retval");
}

Daemon/MPG123.pm  view on Meta::CPAN

  my $self = shift;
  my $remote = shift;
  push @{$self->{playlist}}, @{$remote->{args}};
  # create array of indexes: 
  my @trandom = map { $_ } (0..$#{$self->{playlist}});
  $self->randomize(\@trandom);
  # if we have a "new" list, start playing otherwise continue on
  if (scalar @{$self->{playlist}} == scalar @{$remote->{args}}) {
    if ($self->{state}{random}) {
      $self->{state}{ranid} = 0;
      $self->{state}{regid} = $self->{random}[0];
    } else {
      $self->{state}{regid} = 0;
      $self->{state}{ranid} = $self->{revrandom}[0];
    }
    # $self->debug("Setting regid to ".$self->{state}{regid}." and ranid to ".$self->{state}{ranid});
    $self->play;
  }
}
sub del {
  my $self = shift;
  my $remote = shift;
  my $player = $self->player;
  if ($remote->{args}[0] eq 'all') {
    $self->{playlist} = [];
    $player->stop;
  } elsif (scalar @{$remote->{args}}) {
    my @args = sort { $b <=> $a } @{$remote->{args}};
    foreach my $index (@args) {
      splice(@{$self->{playlist}}, $index, 1);
    }
    my @trandom = map { $_ } (0..$#{$self->{playlist}});
    $self->randomize(\@trandom);
    if (scalar grep {/^$self->{state}{regid}$/} @args) {
      $self->next;
    }
  }
}

sub next {
  my $self = shift;
  my $remote = shift;
  # $self->debug("Random and repeat: ".$self->{state}{random}."/".$self->{state}{repeat});

  my $callplay = 0;
  my $id;
  if ($self->{state}{random}) {
    # $self->debug("Taking random ID ".$self->{state}{ranid}." to move forward on");
    $id = $self->{state}{ranid};
  } else {
    # $self->debug("Taking straight ID ".$self->{state}{regid}." to move forward on");
    $id = $self->{state}{regid};
  }
  $id++;
  if ($id > $#{$self->{playlist}}) {
    # $self->debug("end of playlist found");
    $id = 0;
    if ((ref $remote && $remote->{cmd} eq 'next') || $self->{state}{repeat}) {
      $callplay = 1 if ($self->{state}{state} != 0);
    }
  } else {
    $callplay = 1 if ($self->{state}{state} != 0);
  }
  if ($self->{state}{random}) {
    # $self->debug("assigning $id back to random ID");
    $self->{state}{ranid} = $id;
    $self->{state}{regid} = $self->{random}[$id];
  } else {
    # $self->debug("assigning $id back to regular ID");
    $self->{state}{regid} = $id;
    $self->{state}{ranid} = $self->{revrandom}[$id];
  }
  $self->play if ($callplay);
}

sub prev {
  my $self = shift;
  my $remote = shift;
  # $self->debug("Random and repeat: ".$self->{state}{random}."/".$self->{state}{repeat});

  my $id;
  if ($self->{state}{random}) {
    # $self->debug("Taking random ID ".$self->{state}{ranid}." to move back on");
    $id = $self->{state}{ranid};
  } else {
    # $self->debug("Taking straight ID ".$self->{state}{regid}." to move back on");
    $id = $self->{state}{regid};
  }
  $id--;
  if ($id < 0) {
    # $self->debug("beyond beginning of playlist found");
    $id = $#{$self->{playlist}};
  }
  if ($self->{state}{random}) {
    # $self->debug("assigning $id back to random ID");
    $self->{state}{ranid} = $id;
    $self->{state}{regid} = $self->{random}[$id];
  } else {
    # $self->debug("assigning $id back to regular ID");
    $self->{state}{regid} = $id;
    $self->{state}{ranid} = $self->{revrandom}[$id];
  }
  $self->play if ($self->{state}{state} != 0);
}

sub random {
  my $self = shift;
  my $remote = shift;
  my $oldstate = $self->{state}{random};
  # $self->debug("Trying to set ".($remote->{args}[0]));

Daemon/MPG123.pm  view on Meta::CPAN

  my $self = shift;
  my $player = $self->player;
  my @out;
  push @out, "state:".$player->state;
  # 0: stopped   1: Paused  2: Playing
  push @out, "random:".$self->{state}{random};
  push @out, "repeat:".$self->{state}{repeat};
  # push @out, "randomlist:".(join ',', @{$self->{random}});
  # push @out, "revrandomlist:".(join ',', @{$self->{revrandom}});
  # push @out, "randomid:".$self->{state}{ranid};
  push @out, "id:".$self->{state}{regid};
  push @out, "frame:".(join ',', @{$player->{frame}}) if (ref $player->{frame} && scalar @{$player->{frame}});
  push @out, "title:".$player->title;
  push @out, "artist:".$player->artist;
  push @out, "album:".$player->album;
  push @out, "genre:".$player->genre;
  push @out, "url:".$player->url;
  my ($lvol, $rvol) = ($MIXER)?Audio::Mixer::get_cval('vol'):(undef, undef);
  push @out, "vol:$lvol $rvol\n";
  if ($self->{state}{showlist}) {
    push @out, "list:".(join $self->{subsep}, @{$self->{playlist}});

Daemon/MPG123.pm  view on Meta::CPAN

      if (scalar grep {/^$remote->{cmd}$/} @cmds) {
        my $method = '$self->'.$remote->{cmd}.'($remote)';
        eval "$method";
      }
      $self->send_status;
    }
    
    $player->poll(0);
    if ($player->state == 0 && $self->{state}{state} == 2) {
      print "Restarting next song...\n";
      $self->next unless ($self->{state}{regid} == $#{$self->{playlist}} && (! $self->{state}{repeat}));
    } elsif ($player->state == 0) {
      # print "Stopped\n";
    } elsif ($player->state == 1) {
      # print "Paused\n";
    } elsif ($player->state == 2) {
      my ($lvol, $rvol) = Audio::Mixer::get_cval('vol') if ($MIXER);
      my $out = $player->url." $lvol $rvol\n";
      # print "Playing $out";
    }
    if ($self->{state}{state} != $player->state) {

Daemon/Shout.pm  view on Meta::CPAN

  $self->{state}{state} = 0;
}
  
sub play {
  my $self = shift;
  my $remote = shift;
  my $player = $self->player;
  if (defined $remote && ref $remote && scalar @{$remote->{args}}) {
    if ($self->{state}{random}) {
      $self->{state}{renid} = $self->{revrandom}[$remote->{args}[0]];
      $self->{state}{regid} = $remote->{args}[0];
    }
  }
  $self->debug("Random is ".($self->{state}{random})?'true':'false');
  my $id = $self->{state}{regid};
  $self->info("Setting current playlist id to $id and song to ".$self->{playlist}[$id]);
  if (! -r $self->{playlist}[$id]) {
    $self->crit("File Not Found or Not Readable: ".$self->{playlist}[$id]);
    return;
  }
  $self->{state}{state} = 2;
}

sub pause {
  my $self = shift;

Daemon/Shout.pm  view on Meta::CPAN

  my $self = shift;
  my $remote = shift;
  push @{$self->{playlist}}, @{$remote->{args}};
  # create array of indexes: 
  my @trandom = map { $_ } (0..$#{$self->{playlist}});
  $self->randomize(\@trandom);
  # if we have a "new" list, start playing otherwise continue on
  if (scalar @{$self->{playlist}} == scalar @{$remote->{args}}) {
    if ($self->{state}{random}) {
      $self->{state}{ranid} = 0;
      $self->{state}{regid} = $self->{random}[0];
    } else {
      $self->{state}{regid} = 0;
      $self->{state}{ranid} = $self->{revrandom}[0];
    }
    # $self->debug("Setting regid to ".$self->{state}{regid}." and ranid to ".$self->{state}{ranid});
    $self->play;
  }
}

sub del {
  my $self = shift;
  my $remote = shift;
  if ($remote->{args}[0] eq 'all') {
    $self->{playlist} = [];
    $self->{state}{state} = 0;
  } elsif (scalar @{$remote->{args}}) {
    my @args = sort { $b <=> $a } @{$remote->{args}};
    foreach my $index (@args) {
      splice(@{$self->{playlist}}, $index, 1);
    }
    my @trandom = map { $_ } (0..$#{$self->{playlist}});
    $self->randomize(\@trandom);
    if (scalar grep {/^$self->{state}{regid}$/} @args) {
      $self->next;
    }
  }
}

sub next {
  my $self = shift;
  my $remote = shift;
  # $self->debug("Random and repeat: ".$self->{state}{random}."/".$self->{state}{repeat});

  my $callplay = 0;
  my $id;
  if ($self->{state}{random}) {
    # $self->debug("Taking random ID ".$self->{state}{ranid}." to move forward on");
    $id = $self->{state}{ranid};
  } else {
    # $self->debug("Taking straight ID ".$self->{state}{regid}." to move forward on");
    $id = $self->{state}{regid};
  }
  $id++;
  if ($id > $#{$self->{playlist}}) {
    # $self->debug("end of playlist found");
    $id = 0;
    if ((ref $remote && $remote->{cmd} eq 'next') || $self->{state}{repeat}) {
      $callplay = 1 if ($self->{state}{state} != 0);
    }
  } else {
    $callplay = 1 if ($self->{state}{state} != 0);
  }
  if ($self->{state}{random}) {
    # $self->debug("assigning $id back to random ID");
    $self->{state}{ranid} = $id;
    $self->{state}{regid} = $self->{random}[$id];
  } else {
    # $self->debug("assigning $id back to regular ID");
    $self->{state}{regid} = $id;
    $self->{state}{ranid} = $self->{revrandom}[$id];
  }
  if ($callplay) {
    $self->closefile;
    $self->initfile;
  }
}

sub prev {
  my $self = shift;
  my $remote = shift;
  # $self->debug("Random and repeat: ".$self->{state}{random}."/".$self->{state}{repeat});

  my $id;
  if ($self->{state}{random}) {
    # $self->debug("Taking random ID ".$self->{state}{ranid}." to move back on");
    $id = $self->{state}{ranid};
  } else {
    # $self->debug("Taking straight ID ".$self->{state}{regid}." to move back on");
    $id = $self->{state}{regid};
  }
  $id--;
  if ($id < 0) {
    # $self->debug("beyond beginning of playlist found");
    $id = $#{$self->{playlist}};
  }
  if ($self->{state}{random}) {
    # $self->debug("assigning $id back to random ID");
    $self->{state}{ranid} = $id;
    $self->{state}{regid} = $self->{random}[$id];
  } else {
    # $self->debug("assigning $id back to regular ID");
    $self->{state}{regid} = $id;
    $self->{state}{ranid} = $self->{revrandom}[$id];
  }
  if ($self->{state}{state} != 0) {
    $self->closefile;
    $self->initfile;
  }
}

sub random {
  my $self = shift;

Daemon/Shout.pm  view on Meta::CPAN

  my $self = shift;
  # my $player = $self->player;
  my @out;
  push @out, "state:".$self->{state}{state};
  # 0: stopped   1: Paused  2: Playing
  push @out, "random:".$self->{state}{random};
  push @out, "repeat:".$self->{state}{repeat};
  # push @out, "randomlist:".(join ',', @{$self->{random}});
  # push @out, "revrandomlist:".(join ',', @{$self->{revrandom}});
  # push @out, "randomid:".$self->{state}{ranid};
  push @out, "id:".$self->{state}{regid};
  # push @out, "frame:".(join ',', @{$player->{frame}}) if (ref $player->{frame} && scalar @{$player->{frame}});
  my $file = $self->{playlist}[$self->{state}{regid}];
  my $info = get_mp3info($file);
  my $tag = get_mp3tag($file);
  my $perc = $self->{state}{curbyte}/$info->{SIZE};
  my $ttime = $info->{SECS}.'.'.$info->{MS};
  my $passed = $ttime * $perc;
  push @out, "frame:".(join ',', $self->{state}{curbyte}, $info->{SIZE}, 
                      sprintf("%.2f", $passed), sprintf("%.2f", $ttime));
  
  push @out, 'rateinfo:'.(join ',', $info->{BITRATE}, $info->{FREQUENCY}, ($info->{STEREO}+1));                                   
  if (defined $tag && ref $tag) {

Daemon/Shout.pm  view on Meta::CPAN

    $currentstate = $self->{state}{state};
  }
}

sub initfile {
  my $self = shift;
  my $player = $self->player;
  if (defined $self->{_fh}) {
    $self->closefile;
  }
  my $file = $self->{playlist}[$self->{state}{regid}];
  if (! -r $file) {
    $self->crit("unable to find/read $file");
    return;
  }
  if (defined $self->{Pass}{lame}) {
    $self->info("trying to load file: $file and downsample to ".$self->{Pass}{bitrate});
    open( $self->{_fh}, "-|" ) ||
    exec $self->{Pass}{lame}, qw{--mp3input -b}, $self->{Pass}{bitrate}, qw{-m j -h -S}, $file, "-";
    if (! defined $self->{_fh}) {
      $self->crit("Failed to downsample $file: $!");

Daemon/Shout.pm  view on Meta::CPAN

sub send_chunk {
  my $self = shift;
  return if (! defined $self->{_fh});
  my $player = $self->player;
  
  $self->{Pass}{chunk} = $self->{Pass}{chunk} || 2048;
  my $buff;
  my $len = sysread($self->{_fh}, $buff, $self->{Pass}{chunk});
  if ($len == 0 || ! defined $len) {
    $self->closefile;
    if ($self->{state}{regid} == $#{$self->{playlist}} && (! $self->{state}{repeat})) {
     $self->{state}{state} = 0;
    } else {
     $self->next;
     $self->initfile;
     $self->send_chunk;
    }
  } else {
    $self->{state}{curbyte} += $len;
    unless ($player->sendData($buff)) {
      $self->crit("send failed: ".$player->error);



( run in 0.611 second using v1.01-cache-2.11-cpan-5735350b133 )