-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sf2_Batch_Recording
executable file
·235 lines (201 loc) · 6.75 KB
/
Sf2_Batch_Recording
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/bin/bash
################################################################################
clear && printf "Welcome to Sf2_Batch_Recording
this script let you make some nice wave files from sf2
it is only a bash script for making some samples for the SamplerBox including definition
text file.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! !
! Note, this will take a long time. A simple example: !
! !
! This script needs about 1 second to run. Because of waiting till fluidsynth is started!
! and sendmidi is connected. This will happen every note. !
! !
! You want have 127 notes in one velocity and every sample will have 14 seconds !
! !
! 14 sec sample + 1 sec script x 127 samples / 60 sec = 32 Minutes ! !
! !
! Test it first with one or two notes !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
"
################################################################################
### Some software tests #######################################################
if [[ -z $(screen -v 2>&1 | sed '/command not found/d') ]]
then
printf 'screen is not installed, try to install it via homebrew'
exit 0
fi
if [[ -z $(fluidsynth -V 2>&1 | sed '/command not found/d') ]]
then
printf 'fluidsynth is not installed, try to install it via homebrew'
exit 0
fi
if [[ -z $(sendmidi --version 2>&1 | sed '/command not found/d') ]]
then
printf 'sendmidi is not installed, try to install it via homebrew'
exit 0
fi
if [[ -z $(sox --version 2>&1 | sed '/command not found/d') ]]
then
printf 'sox is not installed, try to install it via homebrew'
exit 0
fi
### Set folder for banks, read content and ask user to choose one ##############
BKFOLDER='/Library/Audio/Sounds/Banks/'
ls $BKFOLDER > /var/tmp/Banks_
cat -n /var/tmp/Banks_
printf '\nChoose a soundbank in integer:'
read CHOOSEBANK
if [[ -z "$CHOOSEBANK" ]]
then
printf "Soundbank required....exiting\n"
exit 0
fi
SOUNDBANK=$(tail +$CHOOSEBANK /var/tmp/Banks_ | head -1)
BKFOLDER=$BKFOLDER""$SOUNDBANK
echo $BKFOLDER
SOUNDBANK=$(echo $SOUNDBANK | cut -d "." -f 1 ) # this is a small fix to make the folder name more beautiful
mkdir $SOUNDBANK
### Set velocity and make the folder ###########################################
y=-1
while [[ "$y" -lt 0 || "$y" -gt 127 ]]
do
printf '\nChoose a velocity between 0 and 127 or press enter for 127: '
read y
if [[ -z $y ]]
then
y=127
fi
done
printf "The velocity is set to $y\n"
### define key range ###########################################################
a=-1
while [[ "$a" -lt 0 || "$a" -gt 127 ]]
do
printf '\nChoose the beginning key no. between 0 and 127 or press enter for 0: '
read a
if [[ -z $a ]]
then
a=0
fi
done
printf "We will begin with key $a\n"
b=-1
while [[ "$b" -lt 0 || "$b" -gt 127 ]]
do
printf '\nChoose the end key no. between 0 and 127 or press enter for 127: '
read b
if [[ -z $b ]]
then
b=127
fi
done
printf "We will end with key $b\n"
b=$[$b+1]
### define sample length #######################################################
while [[ "$time" -lt 1 ]]
do
printf '\nChoose the sample length in full seconds(i will do 13 seconds): '
read time
done
printf "Sample length will be $time seconds\n"
### Debug play #################################################################
printf '\nDo you want to play every note past conversion? This will double the duration.
1 or enter for no '
read debug
### Fluidsynth Gain ############################################################
printf '\nPlease enter gain for fluidsynth between 0 and 5, Or press enter for gain 0.8: '
read fgain
if [[ -z $fgain ]]
then
fgain=0.8
fi
printf "Gain is set to $fgain\n"
### Sync Options ###############################################################
printf '\nHere you can cut of the file from beginning. In my mac I have a litte bit silence
on the beginning of the file for 0.1 second. That will be the default value if you press enter.
But feel free to change it to your wish. But stay in a positive value.
Type a value or press enter for my default 0.1 seconds: '
read sync
if [[ -z $sync ]]
then
sync=0.1
fi
printf "\nOk. we will cut another $sync off of the file\n"
### Effects ####################################################################
EFFECTS_FOLDER='effects/'
ls $EFFECTS_FOLDER > /var/tmp/effects_
cat -n /var/tmp/effects_
printf '\nChoose an effect in integer:'
read CHOOSE_EFFECT
if [[ -z "$CHOOSE_EFFECT" ]]
then
EFFECTS=""
printf 'recording without effects.\n'
else
EFFECTS=$(tail +$CHOOSE_EFFECT /var/tmp/effects_ | head -1)
printf "$EFFECTS will be used.\n"
fi
### Definition text file #######################################################
printf 'Writing definition file\n'
echo "%midinote_%velocity.wav" > $SOUNDBANK/definition.txt
### Working zone ###############################################################
caffeinate -dim &
while [[ "$a" != "$b" ]]
do
### Fluidsynth #########################################################
filename=$a"_"$y
printf 'start FluidSynth'
screen -d -m fluidsynth -g $fgain -a file $BKFOLDER -o audio.file.name=$SOUNDBANK/temp.raw
fluidsynth_is_running=""
while [[ -z "$fluidsynth_is_running" ]]
do
printf '.'
fluidsynth_is_running=$(pgrep fluidsynth > $SOUNDBANK/fluidsynth.pid && echo "1")
done
### Sendmidi ###########################################################
printf 'wait for sendmidi'
sendmidi dev fluidsynth 2> /var/tmp/sendmidi_con
sle=$(cat /var/tmp/sendmidi_con)
while [[ "$sle" != "" ]]
do
printf '.'
sendmidi dev fluidsynth 2> /var/tmp/sendmidi_con
sle=$(cat /var/tmp/sendmidi_con)
done
printf "sending tone -key $a -velocity $y for $time sec\n"
sendmidi dev fluidsynth on $a $y &
sleep $time
printf 'kill fluidsynth...'
killall fluidsynth
a=$[$a+1]
### Sox ################################################################
printf 'converting soundsample...'
sox -r 44100 -c 2 -b 16 -e signed-integer $SOUNDBANK/temp.raw $SOUNDBANK/temp.wav
file_length=$(sox $SOUNDBANK/temp.wav -n stat 2>&1 |grep Length |sed -n 's#^Length (seconds):[^0-9]*\([0-9.]*\)$#\1#p')
z=$(echo "$file_length-$time+$sync" | bc)
printf "trim the file from $z "
sox $SOUNDBANK/temp.wav $SOUNDBANK/$filename.wav trim $z $(< effects/$EFFECTS)
rm $SOUNDBANK/temp.raw
printf 'done\n'
if [[ $debug = 1 ]]
then
play $SOUNDBANK/$filename.wav
fi
done
killall caffeinate 1>&/dev/null &
### cleaning files ##############################################################
rm /var/tmp/Banks_
rm $SOUNDBANK/temp.*
rm $SOUNDBANK/fluidsynth.pid
unset BKFOLDER
unset SOUNDBANK
unset CHOOSEBANK
unset time
unset a
unset b
unset y
unset z
unset sle
unset debug
unset fgain