|
| 1 | +package Plugins::SigGen::Plugin; |
| 2 | + |
| 3 | +# Signal Generator Plugin |
| 4 | + |
| 5 | +use strict; |
| 6 | + |
| 7 | +use base qw(Slim::Plugin::Base); |
| 8 | + |
| 9 | +use Slim::Utils::Strings qw(string); |
| 10 | +use Slim::Utils::Prefs; |
| 11 | + |
| 12 | +use Plugins::SigGen::ProtocolHandler; |
| 13 | + |
| 14 | +sub getDisplayName { 'PLUGIN_SIGGEN' }; |
| 15 | + |
| 16 | +sub modeName { 'PLUGIN_SIGGEN' }; |
| 17 | + |
| 18 | +my $prefs = preferences('server'); |
| 19 | + |
| 20 | +# following are the setting which are changed from the remote with button presses |
| 21 | +my @funcs = ( 'sine', 'square', 'triangle', 'sawtooth' ); # button 1 |
| 22 | +my @amps = ( -80, -60, -40, -20, -10, -6, 0 ); # button 2 inc, button 5 dec |
| 23 | +my @chans = ( 'l+r', 'l', 'r' ); # button 3 |
| 24 | +my @bits = ( 16, 24 ); # button 4 |
| 25 | +my @rates = ( 8, 11, 12, 16, 22, 24, 32, 44, 48, 88, 96 ); # button 6 |
| 26 | + |
| 27 | +sub setMode { |
| 28 | + my $class = shift; |
| 29 | + my $client = shift; |
| 30 | + |
| 31 | + my $s = $client->modeParam('params') || { |
| 32 | + 'freq' => 500, |
| 33 | + 'func' => 0, |
| 34 | + 'chan' => 0, |
| 35 | + 'amp' => 2, |
| 36 | + 'bits' => 0, |
| 37 | + 'rate' => 7, |
| 38 | + }; |
| 39 | + |
| 40 | + $client->modeParam('params', $s); |
| 41 | + |
| 42 | + $client->lines(\&lines); |
| 43 | + |
| 44 | + Slim::Utils::Timers::setTimer($client, Time::HiRes::time + 1.0, \&update); |
| 45 | + |
| 46 | + avoidScreenSaver($client); |
| 47 | +} |
| 48 | + |
| 49 | +sub exitMode { |
| 50 | + my $class = shift; |
| 51 | + my $client = shift; |
| 52 | + |
| 53 | + $client->execute(['playlist', 'clear']); |
| 54 | + |
| 55 | + for my $track (Slim::Schema->rs('Track')->search_like({ 'url' => 'siggen%' })->all) { |
| 56 | + $track->delete; |
| 57 | + } |
| 58 | + |
| 59 | + $client->display->updateMode(0); |
| 60 | +} |
| 61 | + |
| 62 | +sub lines { |
| 63 | + my $client = shift; |
| 64 | + |
| 65 | + my $s = $client->modeParam('params'); |
| 66 | + |
| 67 | + return { |
| 68 | + 'line' => [ string('PLUGIN_SIGGEN'), string('PLUGIN_SIGGEN_' . uc $funcs[$s->{'func'}]) ], |
| 69 | + 'overlay' => [ |
| 70 | + (sprintf "%s %s/%s", string('PLUGIN_SIGGEN_' . uc $chans[$s->{'chan'}]), $rates[$s->{'rate'}], $bits[$s->{'bits'}]), |
| 71 | + (sprintf "%s dB %s Hz", $amps[$s->{'amp'}], $s->{'freq'}), |
| 72 | + ], |
| 73 | + }; |
| 74 | +} |
| 75 | + |
| 76 | +my %functions = ( |
| 77 | + 'left' => sub { Slim::Buttons::Common::popModeRight(shift) }, |
| 78 | + |
| 79 | + 'right'=> sub { shift->bumpRight }, |
| 80 | + |
| 81 | + 'up' => sub { |
| 82 | + my $client = shift; |
| 83 | + my $s = $client->modeParam('params'); |
| 84 | + |
| 85 | + if ($s->{'freq'} < 20000) { |
| 86 | + |
| 87 | + $s->{'freq'} += ($s->{'freq'} >= 100 ? 100 : 10); |
| 88 | + |
| 89 | + update($client); |
| 90 | + } |
| 91 | + }, |
| 92 | + |
| 93 | + 'down' => sub { |
| 94 | + my $client = shift; |
| 95 | + my $s = $client->modeParam('params'); |
| 96 | + |
| 97 | + if ($s->{'freq'} > 10) { |
| 98 | + |
| 99 | + $s->{'freq'} -= ($s->{'freq'} > 100 ? 100 : 10); |
| 100 | + |
| 101 | + update($client); |
| 102 | + } |
| 103 | + }, |
| 104 | + |
| 105 | + 'jump' => sub { |
| 106 | + my ($client, $funct, $arg) = @_; |
| 107 | + my $s = $client->modeParam('params'); |
| 108 | + |
| 109 | + if ($arg eq 'rew') { |
| 110 | + $s->{'freq'} = $s->{'freq'} > 100 ? $s->{'freq'} / 10 : $s->{'freq'}; |
| 111 | + } |
| 112 | + |
| 113 | + if ($arg eq 'fwd') { |
| 114 | + $s->{'freq'} = $s->{'freq'} <= 2000 ? $s->{'freq'} * 10 : $s->{'freq'}; |
| 115 | + } |
| 116 | + |
| 117 | + update($client); |
| 118 | + }, |
| 119 | + |
| 120 | + 'numberScroll' => sub { |
| 121 | + my ($client, $funct, $arg) = @_; |
| 122 | + |
| 123 | + my $s = $client->modeParam('params'); |
| 124 | + |
| 125 | + if ($arg eq '1') { |
| 126 | + $s->{'func'} = ($s->{'func'} + 1) % scalar @funcs; |
| 127 | + } |
| 128 | + |
| 129 | + if ($arg eq '2') { |
| 130 | + $s->{'amp'} = $s->{'amp'} < $#amps ? $s->{'amp'} + 1 : $s->{'amp'}; |
| 131 | + } |
| 132 | + |
| 133 | + if ($arg eq '3') { |
| 134 | + $s->{'chan'} = ($s->{'chan'} + 1) % scalar @chans; |
| 135 | + } |
| 136 | + |
| 137 | + if ($arg eq '4') { |
| 138 | + $s->{'rate'} = ($s->{'rate'} + 1) % scalar @rates; |
| 139 | + if ($rates[$s->{'rate'}] * 1000 > $client->maxSupportedSamplerate) { |
| 140 | + $s->{'rate'} = 0; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + if ($arg eq '5') { |
| 145 | + $s->{'amp'} = $s->{'amp'} > 0 ? $s->{'amp'} - 1 : 0; |
| 146 | + } |
| 147 | + |
| 148 | + if ($arg eq '6') { |
| 149 | + $s->{'bits'} = ($s->{'bits'} + 1) % scalar @bits; |
| 150 | + } |
| 151 | + |
| 152 | + update($client); |
| 153 | + } |
| 154 | +); |
| 155 | + |
| 156 | +sub getFunctions { |
| 157 | + return \%functions; |
| 158 | +} |
| 159 | + |
| 160 | +sub update { |
| 161 | + my $client = shift; |
| 162 | + |
| 163 | + my $s = $client->modeParam('params'); |
| 164 | + |
| 165 | + my $freq = $s->{'freq'}; |
| 166 | + my $func = $funcs[$s->{'func'}]; |
| 167 | + my $rate = $rates[$s->{'rate'}]; |
| 168 | + my $bits = $bits[$s->{'bits'}]; |
| 169 | + my $ampL = ($chans[$s->{'chan'}] =~ /l/) ? $amps[$s->{'amp'}] : 'off'; |
| 170 | + my $ampR = ($chans[$s->{'chan'}] =~ /r/) ? $amps[$s->{'amp'}] : 'off'; |
| 171 | + |
| 172 | + my $url = "siggen://test.raw?func=$func&freq=$freq&rate=$rate&bits=$bits&L=$ampL&R=$ampR"; |
| 173 | + |
| 174 | + Slim::Music::Info::setTitle($url, string('PLUGIN_SIGGEN_TESTSIGNAL') ." $func $freq Hz"); |
| 175 | + |
| 176 | + $client->display->updateMode(1); |
| 177 | + $client->showBriefly(lines($client), { 'duration' => 3, 'block' => 1 }); |
| 178 | + |
| 179 | + Slim::Utils::Timers::killTimers($client, \&execute); |
| 180 | + Slim::Utils::Timers::setTimer($client, Time::HiRes::time() + 0.5, \&execute, $url); |
| 181 | +} |
| 182 | + |
| 183 | +sub execute { |
| 184 | + my $client = shift; |
| 185 | + my $url = shift; |
| 186 | + |
| 187 | + $client->execute(['playlist', 'play', $url]); |
| 188 | +} |
| 189 | + |
| 190 | +sub avoidScreenSaver { |
| 191 | + my $client = shift; |
| 192 | + |
| 193 | + my $now = Time::HiRes::time(); |
| 194 | + my $timeout = $prefs->client($client)->get('screensavertimeout') || return; |
| 195 | + |
| 196 | + if (Slim::Buttons::Common::mode($client) eq 'PLUGIN_SIGGEN') { |
| 197 | + |
| 198 | + if ($now - Slim::Hardware::IR::lastIRTime($client) > $timeout / 2) { |
| 199 | + Slim::Hardware::IR::setLastIRTime($client, $now); |
| 200 | + } |
| 201 | + |
| 202 | + Slim::Utils::Timers::setTimer($client, $now + $timeout / 2, \&avoidScreenSaver); |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +1; |
0 commit comments