| 1 | #_{ Encoding and name | ||||||||||
| 2 | =encoding utf8 | ||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | Csound::Orchestra | ||||||||||
| 6 | |||||||||||
| 7 | =cut | ||||||||||
| 8 | #_} | ||||||||||
| 9 | package Csound::Orchestra; | ||||||||||
| 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 | |||||||||||
| 14 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 2 | our $VERSION = $Csound::VERSION; | |||||||||
| 17 | #_{ Synopsis | ||||||||||
| 18 | |||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||
| 20 | |||||||||||
| 21 | use Csound::Orchestra; | ||||||||||
| 22 | |||||||||||
| 23 | my $orchestra=Csound::Orchestra->new(); | ||||||||||
| 24 | |||||||||||
| 25 | =cut | ||||||||||
| 26 | #_} | ||||||||||
| 27 | #_{ Description | ||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||
| 29 | |||||||||||
| 30 | An orchestra consists of L<instruments|Csound::Instrument> | ||||||||||
| 31 | |||||||||||
| 32 | An orchestra should be created by a L<Csound::Score>. | ||||||||||
| 33 | |||||||||||
| 34 | =over | ||||||||||
| 35 | |||||||||||
| 36 | =item * a header section | ||||||||||
| 37 | |||||||||||
| 38 | The L<header section|http://www.csounds.com/manual/html/OrchTop.html#OrchHeader> specifies global options for instrument performance. It is written with L</_write_header>. | ||||||||||
| 39 | |||||||||||
| 40 | =item * an optional list of user defined opcodes | ||||||||||
| 41 | |||||||||||
| 42 | L<User defined opcodes|http://www.csounds.com/manual/html/OrchUDO.html> are built with the I<opcodes> C<< L<opcode|http://www.csounds.com/manual/html/opcode.html> >> and | ||||||||||
| 43 | C<< L<endop|http://www.csounds.com/manual/html/endop.html> >>. | ||||||||||
| 44 | |||||||||||
| 45 | =item * instrument definitions | ||||||||||
| 46 | |||||||||||
| 47 | =back | ||||||||||
| 48 | |||||||||||
| 49 | =cut | ||||||||||
| 50 | #_} | ||||||||||
| 51 | #_{ Methods | ||||||||||
| 52 | =head1 METHODS | ||||||||||
| 53 | =cut | ||||||||||
| 54 | 0 | ▶ | sub new { #_{ | ||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 55 | #_{ POD | ||||||||||
| 56 | =head2 new | ||||||||||
| 57 | |||||||||||
| 58 | An orchestra should not be created by the end user. The user should rather use a L<Csound::Score>. | ||||||||||
| 59 | |||||||||||
| 60 | =cut | ||||||||||
| 61 | #_} | ||||||||||
| 62 | |||||||||||
| 63 | 0 | my $class = shift; | |||||||||
| 64 | |||||||||||
| 65 | 0 | my $self = {}; | |||||||||
| 66 | |||||||||||
| 67 | 0 | bless $self, $class; | |||||||||
| 68 | |||||||||||
| 69 | 0 | ▶ | die unless $self->isa('Csound::Orchestra'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | |||||||||||
| 71 | # An orchestra requires some instruments. | ||||||||||
| 72 | # The key of the hash is the instrument number. | ||||||||||
| 73 | # The instruments are added with the L</UseInstrument>() method. | ||||||||||
| 74 | 0 | $self->{instruments} = {}; | |||||||||
| 75 | |||||||||||
| 76 | 0 | return $self; | |||||||||
| 77 | |||||||||||
| 78 | } #_} | ||||||||||
| 79 | 0 | ▶ | sub use_instrument { | ||||||||
|
Subroutine
use_instrument: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 80 | #_{ POD | ||||||||||
| 81 | =head2 use_instrument | ||||||||||
| 82 | |||||||||||
| 83 | $orc -> use_instrument($instr); | ||||||||||
| 84 | |||||||||||
| 85 | Add $instr to the instruments. An instrument can be added multiple times, for example by L<Csound::Score/play>. | ||||||||||
| 86 | |||||||||||
| 87 | =cut | ||||||||||
| 88 | #_} | ||||||||||
| 89 | |||||||||||
| 90 | 0 | my $self = shift; | |||||||||
| 91 | 0 | my $instr = shift; | |||||||||
| 92 | |||||||||||
| 93 | 0 | ▶ | croak "Not an orchestra " unless $self ->isa('Csound::Orchestra'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 0 | ▶ | croak "Not an instrument" unless $instr->isa('Csound::Instrument'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | |||||||||||
| 96 | 0 | ▶ | $self->{instruments}{$instr->{nr}} = $instr unless exists $self->{instruments}{$instr->{nr}}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | |||||||||||
| 98 | } | ||||||||||
| 99 | 0 | ▶ | sub write { #_{ | ||||||||
|
Subroutine
write: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||
| 100 | #_{ POD | ||||||||||
| 101 | =head2 write | ||||||||||
| 102 | |||||||||||
| 103 | $orc->write('filename.orc', $score); | ||||||||||
| 104 | |||||||||||
| 105 | This method should not be called directly by the end user. The end user should call L<Csound::Score/write> instead. | ||||||||||
| 106 | |||||||||||
| 107 | C<$score> is needed because some instruments need access to the score (notably for the table functions C<f1 8192 10 …>). | ||||||||||
| 108 | |||||||||||
| 109 | =cut | ||||||||||
| 110 | #_} | ||||||||||
| 111 | |||||||||||
| 112 | 0 | my $self = shift; | |||||||||
| 113 | 0 | my $filename_without_suffix = shift; | |||||||||
| 114 | 0 | my $score = shift; | |||||||||
| 115 | |||||||||||
| 116 | 0 | ▶ | croak "$self is a " . ref($self) unless $self->isa('Csound::Orchestra'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 0 | ▶ | croak "score is not defined" unless defined $score; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | 0 | ▶ | croak "score is not a Csound::Score but a $score" unless $score->isa('Csound::Score'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | |||||||||||
| 120 | 0 | ▶ | croak "No filename specified" unless $filename_without_suffix; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | |||||||||||
| 122 | 0 | ▶ | open (my $orc_fh, '>', "$filename_without_suffix.orc") or croak "Could not open $filename_without_suffix.orc"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 123 | |||||||||||
| 124 | 0 | $self->_write_header ($orc_fh); | |||||||||
| 125 | 0 | $self->_write_instruments($orc_fh, $score); | |||||||||
| 126 | |||||||||||
| 127 | 0 | close $orc_fh; | |||||||||
| 128 | |||||||||||
| 129 | } #_} | ||||||||||
| 130 | 0 | ▶ | sub _write_header { #_{ | ||||||||
|
Subroutine
_write_header: not called CC=2 SCAR=17.9
| |||||||||||
| 131 | #_{ POD | ||||||||||
| 132 | =head2 _write_header | ||||||||||
| 133 | |||||||||||
| 134 | An internal function. | ||||||||||
| 135 | |||||||||||
| 136 | L<http://www.csounds.com/manual/html/OrchTop.html#OrchHeader> | ||||||||||
| 137 | |||||||||||
| 138 | =cut | ||||||||||
| 139 | #_} | ||||||||||
| 140 | |||||||||||
| 141 | 0 | my $self = shift; | |||||||||
| 142 | 0 | ▶ | croak unless $self->isa('Csound::Orchestra'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 143 | |||||||||||
| 144 | 0 | my $fh_orc = shift; | |||||||||
| 145 | |||||||||||
| 146 | 0 | print $fh_orc <<HEADER; | |||||||||
| 147 | sr = 44100 | ||||||||||
| 148 | kr = 4410 | ||||||||||
| 149 | ksmps = 10 | ||||||||||
| 150 | nchnls = 2 | ||||||||||
| 151 | ;0dbfs = 1 | ||||||||||
| 152 | HEADER | ||||||||||
| 153 | |||||||||||
| 154 | } #_} | ||||||||||
| 155 | 0 | ▶ | sub _write_instruments { #_{ | ||||||||
|
Subroutine
_write_instruments: not called CC=5 SCAR=34.0
| |||||||||||
| 156 | #_{ POD | ||||||||||
| 157 | =head2 _write_instruments | ||||||||||
| 158 | |||||||||||
| 159 | An internal function. | ||||||||||
| 160 | |||||||||||
| 161 | |||||||||||
| 162 | =cut | ||||||||||
| 163 | #_} | ||||||||||
| 164 | |||||||||||
| 165 | 0 | my $self = shift; | |||||||||
| 166 | 0 | ▶ | croak 'self is not a Csound::Orchestra' unless $self->isa('Csound::Orchestra'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 167 | |||||||||||
| 168 | 0 | my $fh_orc = shift; | |||||||||
| 169 | |||||||||||
| 170 | 0 | my $score = shift; | |||||||||
| 171 | 0 | ▶ | croak "score needed, but is undefined" unless defined $score; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 172 | 0 | ▶ | croak "score needed" unless $score -> isa('Csound::Score'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 173 | |||||||||||
| 174 | 0 | for my $instr_no (sort keys %{$self->{instruments}}) { | |||||||||
| 175 | 0 | print $fh_orc "\n" . $self->{instruments}{$instr_no}->orchestra_text($score); | |||||||||
| 176 | } | ||||||||||
| 177 | |||||||||||
| 178 | |||||||||||
| 179 | } #_} | ||||||||||
| 180 | #_} | ||||||||||
| 181 | #_{ POD: Copyright | ||||||||||
| 182 | |||||||||||
| 183 | =head1 Copyright | ||||||||||
| 184 | |||||||||||
| 185 | Copyright © 2017 René Nyffenegger, Switzerland. All rights reserved. | ||||||||||
| 186 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 187 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||
| 188 | copy of the full license at: L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||
| 189 | |||||||||||
| 190 | =cut | ||||||||||
| 191 | |||||||||||
| 192 | #_} | ||||||||||
| 193 | |||||||||||
| 194 | 2 | 'tq84'; | |||||||||