| 1 | #_{ Encoding and name | ||||||||||||
| 2 | =encoding utf8 | ||||||||||||
| 3 | =head1 NAME | ||||||||||||
| 4 | |||||||||||||
| 5 | Csound::Instrument | ||||||||||||
| 6 | |||||||||||||
| 7 | =cut | ||||||||||||
| 8 | #_} | ||||||||||||
| 9 | package Csound::Instrument; | ||||||||||||
| 10 | |||||||||||||
| 11 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 2 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 2 | ▶ | use 5.10.0; # state | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | 2 | ▶ | use Csound::ScoreStatement::i; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | |||||||||||||
| 18 | 2 | our $VERSION = $Csound::VERSION; | |||||||||||
| 19 | |||||||||||||
| 20 | #_{ Synopsis | ||||||||||||
| 21 | |||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||
| 23 | |||||||||||||
| 24 | use Csound::Instrument; | ||||||||||||
| 25 | |||||||||||||
| 26 | ... | ||||||||||||
| 27 | |||||||||||||
| 28 | =cut | ||||||||||||
| 29 | #_} | ||||||||||||
| 30 | #_{ Description | ||||||||||||
| 31 | =head DESCRIPTION | ||||||||||||
| 32 | |||||||||||||
| 33 | |||||||||||||
| 34 | =cut | ||||||||||||
| 35 | #_} | ||||||||||||
| 36 | #_{ Methods | ||||||||||||
| 37 | #_{ | ||||||||||||
| 38 | =head1 METHODS | ||||||||||||
| 39 | =cut | ||||||||||||
| 40 | #_} | ||||||||||||
| 41 | 6 | ▶ | sub new { #_{ | ||||||||||
|
Subroutine
new: called CC=7 SCAR=19.9
Pod
undocumented
| |||||||||||||
| 42 | #_{ POD | ||||||||||||
| 43 | =head2 new | ||||||||||||
| 44 | |||||||||||||
| 45 | my $composition = Csound::Composition->new(…); | ||||||||||||
| 46 | |||||||||||||
| 47 | my $instr = Csound::Instrument->new( | ||||||||||||
| 48 | { | ||||||||||||
| 49 | composition => $composition, | ||||||||||||
| 50 | parameters => ['amplitude', 'foo_1', 'foo_2'] | ||||||||||||
| 51 | } | ||||||||||||
| 52 | ); | ||||||||||||
| 53 | |||||||||||||
| 54 | If the parameter C<composition> is passed with a reference to a L<< Csound::Composition >>, the instrument's L</play> method is shorthand for | ||||||||||||
| 55 | C<< $composition->play($instr, …) >>. | ||||||||||||
| 56 | |||||||||||||
| 57 | Most instrument play notes. However, to indicate that an instrument doesn't play a note (such as a high hat or a noise etc.), | ||||||||||||
| 58 | the flag C<no_note> can be given. | ||||||||||||
| 59 | |||||||||||||
| 60 | my $instr = Csound::Instrument->new( | ||||||||||||
| 61 | $composition, | ||||||||||||
| 62 | { | ||||||||||||
| 63 | parameters => ['amplitude', 'foo_1', 'foo_2'], | ||||||||||||
| 64 | no_note => 1 | ||||||||||||
| 65 | } | ||||||||||||
| 66 | ); | ||||||||||||
| 67 | |||||||||||||
| 68 | =cut | ||||||||||||
| 69 | #_} | ||||||||||||
| 70 | |||||||||||||
| 71 | 6 | state $_instr_no = 0; | |||||||||||
| 72 | |||||||||||||
| 73 | 6 | my $class = shift; | |||||||||||
| 74 | 6 | ▶ | my $params = shift // {}; | ||||||||||
|
Condition100%2/2
shift() // {}
MC/DC100%1/1
shift() // {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||
| 75 | |||||||||||||
| 76 | 6 | my $self = {}; | |||||||||||
| 77 | |||||||||||||
| 78 | 6 | ▶ | if ($params->{parameters}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | $self->{parameters} = delete $params->{parameters} | ||||||||||||
| 80 | 2 | } | |||||||||||
| 81 | else { | ||||||||||||
| 82 | 4 | $self->{parameters} = []; | |||||||||||
| 83 | } | ||||||||||||
| 84 | 6 | ▶ | if ($params->{composition}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 85 | 0 | ▶ | croak "composition must be a Csound::Composition" unless $params->{composition}->isa('Csound::Composition'); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 86 | 0 | $self->{composition} = delete $params->{composition}; | |||||||||||
| 87 | } | ||||||||||||
| 88 | |||||||||||||
| 89 | 6 | ▶ | $self->{no_note} = delete $params->{no_note} // 0; | ||||||||||
|
Condition100%2/2
delete $params->{"no_note"} // 0
MC/DC100%1/1
delete $params->{"no_note"} // 0
delete $params->{"no_note"}
Truth table100%2/2
A
A= delete $params->{"no_note"}
| |||||||||||||
| 90 | |||||||||||||
| 91 | 6 | bless $self, $class; | |||||||||||
| 92 | |||||||||||||
| 93 | 6 | $self->{nr} = ++$_instr_no; | |||||||||||
| 94 | |||||||||||||
| 95 | 6 | ▶ | $self->definition(delete $params->{definition}) if $params->{definition}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 96 | |||||||||||||
| 97 | 6 | return $self; | |||||||||||
| 98 | |||||||||||||
| 99 | } #_} | ||||||||||||
| 100 | 1 | ▶ | sub definition { #_{ | ||||||||||
|
Subroutine
definition: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 101 | #_{ POD | ||||||||||||
| 102 | =head2 definition | ||||||||||||
| 103 | |||||||||||||
| 104 | =cut | ||||||||||||
| 105 | #_} | ||||||||||||
| 106 | |||||||||||||
| 107 | 1 | my $self = shift; | |||||||||||
| 108 | 1 | my $definition = shift; | |||||||||||
| 109 | |||||||||||||
| 110 | 1 | $self->{definition} = $definition; | |||||||||||
| 111 | } #_} | ||||||||||||
| 112 | 0 | ▶ | sub play { #_{ | ||||||||||
|
Subroutine
play: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 113 | #_{ POD | ||||||||||||
| 114 | =head2 new | ||||||||||||
| 115 | |||||||||||||
| 116 | $instr->play($t_start, $duration, 'f♯5', …); | ||||||||||||
| 117 | |||||||||||||
| 118 | When the instrument was L<constructed|/new> with the C<composition> parameter, this is a shorthand for | ||||||||||||
| 119 | |||||||||||||
| 120 | $composition->play($instr, $t_start, $duration, 'f♯5', …); | ||||||||||||
| 121 | |||||||||||||
| 122 | =cut | ||||||||||||
| 123 | #_} | ||||||||||||
| 124 | |||||||||||||
| 125 | 0 | my $self = shift; | |||||||||||
| 126 | |||||||||||||
| 127 | 0 | ▶ | croak "I don't have a composition to play this instrument on" unless $self->{composition}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 128 | 0 | $self->{composition}->play($self, @_); | |||||||||||
| 129 | |||||||||||||
| 130 | } #_} | ||||||||||||
| 131 | 11 | ▶ | sub plays_note { #_{ | ||||||||||
|
Subroutine
plays_note: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 132 | #_{ POD | ||||||||||||
| 133 | =head2 new | ||||||||||||
| 134 | |||||||||||||
| 135 | my $yes_no = $instr->plays_note(); | ||||||||||||
| 136 | |||||||||||||
| 137 | In most cases, an instrument will play a note. | ||||||||||||
| 138 | When the flag/parameter C<no_note> was given in L</new>, the instrument | ||||||||||||
| 139 | also doesn't play a note. | ||||||||||||
| 140 | |||||||||||||
| 141 | =cut | ||||||||||||
| 142 | #_} | ||||||||||||
| 143 | |||||||||||||
| 144 | 11 | my $self = shift; | |||||||||||
| 145 | |||||||||||||
| 146 | 11 | return ! $self->{no_note}; | |||||||||||
| 147 | |||||||||||||
| 148 | } #_} | ||||||||||||
| 149 | 5 | ▶ | sub i { #_{ | ||||||||||
|
Subroutine
i: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||
| 150 | #_{ POD | ||||||||||||
| 151 | =head2 i | ||||||||||||
| 152 | |||||||||||||
| 153 | $instr -> i($t_start, $t_len, …); | ||||||||||||
| 154 | |||||||||||||
| 155 | Creates an L<i statement|Csound::ScoreStatement::i>. It should not be called by the end user. Rather, the user | ||||||||||||
| 156 | should call L<Csound::Score/play>. | ||||||||||||
| 157 | |||||||||||||
| 158 | =cut | ||||||||||||
| 159 | #_} | ||||||||||||
| 160 | |||||||||||||
| 161 | 5 | my $self = shift; | |||||||||||
| 162 | 5 | my $t_start = shift; | |||||||||||
| 163 | 5 | my $t_len = shift; | |||||||||||
| 164 | 5 | my @params = grep { defined } @_; | |||||||||||
| 165 | |||||||||||||
| 166 | 5 | ▶ | croak unless $self->isa('Csound::Instrument'); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 167 | |||||||||||||
| 168 | 5 | my $expected_param_cnt = @{$self->{parameters}}; | |||||||||||
| 169 | 5 | ▶ | $expected_param_cnt++ if $self->plays_note(); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 170 | |||||||||||||
| 171 | 5 | ▶ | croak (sprintf("expected %d parameters but was given %d", $expected_param_cnt, scalar @params)) unless @params == $expected_param_cnt; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 172 | |||||||||||||
| 173 | 4 | my $i = Csound::ScoreStatement::i->new($self, $t_start, $t_len, @params); | |||||||||||
| 174 | |||||||||||||
| 175 | 4 | return $i; | |||||||||||
| 176 | |||||||||||||
| 177 | } #_} | ||||||||||||
| 178 | 6 | ▶ | sub orchestra_text { #_{ | ||||||||||
|
Subroutine
orchestra_text: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||
| 179 | |||||||||||||
| 180 | #_{ POD | ||||||||||||
| 181 | =head2 orchestra_text | ||||||||||||
| 182 | |||||||||||||
| 183 | my $score = Csound::Score->new(…); | ||||||||||||
| 184 | my $txt = $instr->orchestra_text($score); | ||||||||||||
| 185 | |||||||||||||
| 186 | Returns the text to be written into the score. | ||||||||||||
| 187 | |||||||||||||
| 188 | Sometimes, the instrument needs to have access to the score (notably for the f statements required in the C<oscil> opcode family). Therefore, | ||||||||||||
| 189 | the method needs the C<$score> parameter. | ||||||||||||
| 190 | |||||||||||||
| 191 | =cut | ||||||||||||
| 192 | #_} | ||||||||||||
| 193 | |||||||||||||
| 194 | 6 | my $self = shift; | |||||||||||
| 195 | 6 | my $score = shift; | |||||||||||
| 196 | 6 | ▶ | die unless $self->isa('Csound::Instrument'); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 197 | |||||||||||||
| 198 | 6 | my $orchestra_text = sprintf("instr %d\n\n", $self->{nr}); | |||||||||||
| 199 | |||||||||||||
| 200 | |||||||||||||
| 201 | 6 | my $param_no = 4; | |||||||||||
| 202 | 6 | ▶ | if ($self->plays_note) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 203 | # unless ($self->{no_note}) { | ||||||||||||
| 204 | 4 | $orchestra_text .= " i_freq init cpspch(p4)\n"; | |||||||||||
| 205 | 4 | $param_no++; | |||||||||||
| 206 | } | ||||||||||||
| 207 | |||||||||||||
| 208 | 6 | for my $param (@{$self->{parameters}}) { | |||||||||||
| 209 | 4 | $orchestra_text .= sprintf(" i_%s init p%d\n", $param, $param_no++); | |||||||||||
| 210 | } | ||||||||||||
| 211 | |||||||||||||
| 212 | 6 | ▶ | if ($self->{definition}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 213 | |||||||||||||
| 214 | 1 | my $definition = $self->{definition}; | |||||||||||
| 215 | # $orchestra_text .= $self->{definition}; | ||||||||||||
| 216 | |||||||||||||
| 217 | 1 | $definition =~ s{ | |||||||||||
| 218 | \@FUNCTABLE\(\ *(\d+)((?:\ *,\ *[0-9.]+)+)\ *\) | ||||||||||||
| 219 | } | ||||||||||||
| 220 | { | ||||||||||||
| 221 | 3 | my $gen_no = $1; | |||||||||||
| 222 | 3 | my $parameters = $2; | |||||||||||
| 223 | 3 | $parameters =~ s/^ *, *//; | |||||||||||
| 224 | 3 | my @parameters = split / *, */, $parameters; | |||||||||||
| 225 | # $parameters = join ' x ', @parameters; | ||||||||||||
| 226 | |||||||||||||
| 227 | 3 | ▶ | croak "No score defined" unless defined $score; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 228 | 3 | ▶ | croak "No valid score passed" unless $score->isa('Csound::Score'); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 229 | |||||||||||||
| 230 | 3 | $score->f($gen_no, @parameters)->{table_nr}; | |||||||||||
| 231 | }gex; | ||||||||||||
| 232 | |||||||||||||
| 233 | 1 | $orchestra_text .= $definition; | |||||||||||
| 234 | } | ||||||||||||
| 235 | |||||||||||||
| 236 | # $orchestra_text .= "\n" if @{$self->{parameters}}; | ||||||||||||
| 237 | |||||||||||||
| 238 | 6 | $orchestra_text .= "\nendin\n"; | |||||||||||
| 239 | |||||||||||||
| 240 | 6 | return $orchestra_text; | |||||||||||
| 241 | |||||||||||||
| 242 | } #_} | ||||||||||||
| 243 | #_} | ||||||||||||
| 244 | #_{ POD: Copyright | ||||||||||||
| 245 | |||||||||||||
| 246 | =head1 Copyright | ||||||||||||
| 247 | |||||||||||||
| 248 | Copyright © 2017 René Nyffenegger, Switzerland. All rights reserved. | ||||||||||||
| 249 | This program is free software; you can redistribute it and/or modify it | ||||||||||||
| 250 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||||
| 251 | copy of the full license at: L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||
| 252 | |||||||||||||
| 253 | =cut | ||||||||||||
| 254 | |||||||||||||
| 255 | #_} | ||||||||||||
| 256 | |||||||||||||
| 257 | 2 | 'tq84'; | |||||||||||