A
download bess.rb
Language: Ruby
License: GPL
Copyright: (c) 2002 by Michael Scholz"; (C) 2002 Michael Scholz
LOC: 250
Project Info
Snd
Server: SourceForge
Type: cvs
SourceForge\s\snd\snd\cvs‑snd\
   _new_snd-test.rb
   _sndlib.h
   aclocal.m4
   analog-filter.scm
   audinfo.c
   audio.c
   autosave.scm
   bess.rb
   bess.scm
   bess1.rb
   bess1.scm
   bird.rb
   bird.scm
   clm-ins.rb
   clm-ins.scm
   clm-strings.h
   clm.c
   clm.h
   clm23.scm
   clm2xen.c
   clm2xen.h
   cmn-glyphs.lisp
   config.guess
   config.h.in
   config.rpath
   config.sub
   configure.ac
   debug.scm
   dlocsig.rb
   draw.rb
   draw.scm
   dsp.rb
   dsp.scm
   edit-menu.scm
   edit123.scm
   effects-utils.scm
   effects.rb
   env.rb
   env.scm
   enved.scm
   eval-c.scm
   event.scm
   examp.rb
   examp.scm
   extensions.rb
   extensions.scm
   fade.scm
   fft-menu.scm
   fix-optargs.scm
   fmv.scm
   freeverb.rb
   freeverb.scm
   gettext.h
   gl.c
   grani.scm
   gtk-effects.scm
   gtk-popup.scm
   gui.scm
   headers.c
   help-snd-fm.pd
   help-snd.pd
   HISTORY.Snd
   hooks.rb
   hooks.scm
   index.rb
   index.scm
   inf-snd.el
   io.c
   jcrev.scm
   ladspa-help.scm
   ladspa.scm
   makefile.in
   makefile.no-gettext
   maraca.rb
   maraca.scm
   marks-menu.scm
   marks.rb
   marks.scm
   maxf.rb
   maxf.scm
   midi.c
   misc.scm
   mix-menu.scm
   mix.rb
   mix.scm
   mixer.scm
   moog.scm
   musglyphs.rb
   musglyphs.scm
   nb.rb
   nb.scm
   new-backgrounds.scm
   new-buttons.scm
   new-effects.scm
   new-icons.scm
   noise.rb
   noise.scm
   old-mac-audio.c
   old-snd.spec
   oo.scm
   oscope.scm
   panic.scm
   pd-add.scm
   pd-any.scm
   pd-fm.scm
   pd-global.scm
   pd-inout.scm
   pd-local.scm
   pd-mozilla.scm
   pd-send_receive.scm
   peak-env.scm
   piano.rb
   piano.scm
   play.rb
   play.scm
   poly.rb
   poly.scm
   popup.rb
   popup.scm
   prc95.rb
   prc95.scm
   pvoc.rb
   pvoc.scm
   README.Snd
   rgb.rb
   rgb.scm
   rmsgain.scm
   rt-compiler.scm
   rt-engine.scm
   rt-examples.scm
   rt.tex
   rtio.rb
   rtio.scm
   rubber.rb
   rubber.scm
   saw.c
   singer.rb
   singer.scm
   snd-0.h
   snd-1.h
   snd-axis.c
   snd-chn.c
   snd-completion.c
   snd-dac.c
   snd-data.c
   snd-draw.c
   snd-edits.c
   snd-env.c
   snd-error.c
   snd-fft.c
   snd-file.c
   snd-file.h
   snd-find.c
   snd-g0.h
   snd-g1.h
   snd-gchn.c
   snd-gdraw.c
   snd-gdrop.c
   snd-genv.c
   snd-gfft.c
   snd-gfile.c
   snd-gfind.c
   snd-ghelp.c
   snd-gl.scm
   snd-glistener.c
   snd-gmain.c
   snd-gmenu.c
   snd-gmix.c
   snd-gprefs.c
   snd-gprint.c
   snd-grec.c
   snd-gregion.c
   snd-gsnd.c
   snd-gtk.scm
   snd-gutils.c
   snd-gxbitmaps.c
   snd-gxcolormaps.c
   snd-gxen.c
   snd-gxutils.c
   snd-help.c
   snd-hobbit.scm
   snd-io.c
   snd-kbd.c
   snd-ladspa.c
   snd-listener.c
   snd-main.c
   snd-marks.c
   snd-menu.c
   snd-menu.h
   snd-mix.c
   snd-motif.scm
   snd-nogui.c
   snd-nogui0.h
   snd-nogui1.h
   snd-prefs.c
   snd-print.c
   snd-rec.c
   snd-rec.h
   snd-region.c
   snd-run.c
   snd-select.c
   snd-sig.c
   snd-snd.c
   snd-strings.h
   snd-trans.c
   snd-utils.c
   snd-x0.h
   snd-x1.h
   snd-xchn.c
   snd-xdraw.c
   snd-xdrop.c
   snd-xen.c
   snd-xenv.c
   snd-xfft.c
   snd-xfile.c
   snd-xfind.c
   snd-xhelp.c
   snd-xlistener.c
   snd-xm.rb
   snd-xmain.c
   snd-xmenu.c
   snd-xmix.c
   snd-xprefs.c
   snd-xprint.c
   snd-xrec.c
   snd-xref.c
   snd-xregion.c
   snd-xsnd.c
   snd-xutils.c
   snd-xxen.c
   snd.1
   Snd.ad
   snd.c
   Snd.gtkrc
   snd.h
   snd.spec
   snd_conffile.scm
   snd_pd_external.c
   snd_pd_external.h
   snd4.scm
   snd5.scm
   snd6.scm
   snd7.scm
   sndctrl.c
   sndinfo.c
   sndlib-strings.h
   sndlib.h.in
   sndlib2xen.c
   sndlib2xen.h
   sndplay.c
   sndrecord.c
   sndwarp.scm
   sound.c
   special-menu.scm
   spectr.rb
   spectr.scm
   strad.rb
   strad.scm
   TODO.Snd
   track-colors.scm
   v.rb
   v.scm
   vct.c
   vct.h
   ws.rb
   ws.scm
   xen.c
   xen.h
   xg-x11.h
   xm-enved.rb
   xm-enved.scm
   zip.rb
   zip.scm

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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env ruby -w
# bess -- Translation of Bill Schottstaedt's bess.scm to Ruby.

# Copyright (C) 2002 Michael Scholz

# Author: Michael Scholz <scholz-micha@gmx.de>
# Created: Sun Sep 15 19:11:12 CEST 2002
# Last: Sun Oct 13 05:33:50 CEST 2002
# Version: $Revision: 1.2 $
# Keywords: sound, snd, clm

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.

# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.

# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA

# Code:

DEBUG = true;
VER = "$Revision: 1.2 $";
FILE = File::basename($0);
BANNER = "This is #{FILE.upcase} v#{VER.scan(/[\d.]+/)}, (c) 2002 by Michael Scholz";

# options
$vers = false;
$help = false;
$play = 0.0;
$freq = 220.0;
$index = 1.0;
$ratio = 1;
$amp = 0.5;
$sliderback = "lightsteelblue";
$background = "lightsteelblue1";

# needed by usage()
$low_freq = 40.0;
$high_freq = 2000.0;
$high_index = 3.0;
$high_ratio = 10;

def usage
  print <<USAGE;
#{BANNER}

#{FILE.capitalize} is a Ruby script working with sndlib.so and
libxm.so which must be in the Ruby library path, for example in
/usr/local/lib/ruby/site_ruby, or the environment variable $RUBYLIB
must be set correctly. It opens the DAC, creates some scale widgets,
and starts two CLM oscillators doing frequency modulation in
semi-real-time. This is a translation of bess.scm of Bill
Schottstaedt\'s Snd sound editor.

Default values shown in brackets.

Usage: #{FILE} [ options ] [ -- X options ]

   -p, --play               play immediately (#{$play != 0.0 ? "yes" : "no"})

   -f, --frequency NUMBER   frequency between #{$low_freq} and #{$high_freq} (#{$freq})
   -i, --index NUMBER       index between 0 and #{$high_index} (#{$index})
   -r, --ratio NUMBER       ratio between 0 and #{$high_ratio} (#{$ratio})
   -a, --amplitude NUMBER   amplitude between 0 and 1 (#{$amp})

   -b  --background COLOR   background color (#{$background})
   -s, --sliderback COLOR   slider background color (#{$sliderback})

   -V, --version            display version information and exit
   -h, -?, --help           display this help message and exit

Example: #{FILE} -pf1000 -r3 -b ivory1 -s ivory3
USAGE
  exit(1);
end

def version
  print <<VERSION;
#{BANNER}
#{FILE.capitalize} comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of #{FILE.capitalize}
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.
VERSION
  exit(1);
end

def warn(str = "Warning")
  STDERR.printf("%s%s\n", str, $! ? ": #{$!}" : "");
  STDERR.print("[#{$@}]\n") if $@ and DEBUG;
end

def die(str = "Error", n = 1)
  warn(str);
  exit(n);
end

def get_color(color)
  col = RXColor();
  dpy = RXtDisplay($shell_app[0]);
  cmap = RDefaultColormap(dpy, RDefaultScreen(dpy));
  warn("Can't allocate \"#{color}\"!") if RXAllocNamedColor(dpy, cmap, color, col, col) == 0;
  Rpixel(col);
end

def set_label(w, str)
  RXtVaSetValues(w, [RXmNlabelString, RXmStringCreate(str, RXmFONTLIST_DEFAULT_TAG)]);
end

def make_label(name, alignwidget, form)
  RXtCreateManagedWidget(name, RxmLabelWidgetClass, form,
			 [RXmNleftAttachment, RXmATTACH_FORM,
			   RXmNbottomAttachment, RXmATTACH_NONE,
			   RXmNtopAttachment, RXmATTACH_WIDGET,
			   RXmNtopWidget, alignwidget,
			   RXmNrightAttachment, RXmATTACH_NONE,
			   RXmNalignment, RXmALIGNMENT_END,
			   RXmNwidth, 114,
			   RXmNrecomputeSize, false,
			   RXmNbackground, get_color($background)]);
end

def make_scale_label(alignwidget, form)
  RXtCreateManagedWidget("label", RxmLabelWidgetClass, form,
			 [RXmNleftAttachment, RXmATTACH_WIDGET,
			   RXmNleftWidget, alignwidget,
			   RXmNbottomAttachment, RXmATTACH_NONE,
			   RXmNtopAttachment, RXmATTACH_OPPOSITE_WIDGET,
			   RXmNtopWidget, alignwidget,
			   RXmNrightAttachment, RXmATTACH_NONE,
			   RXmNbackground, get_color($background)]);
end

def make_scale(alignwidget, form)
  RXtCreateManagedWidget("scale", RxmScaleWidgetClass, form,
			 [RXmNleftAttachment, RXmATTACH_WIDGET,
			   RXmNleftWidget, alignwidget,
			   RXmNbottomAttachment, RXmATTACH_NONE,
			   RXmNtopAttachment, RXmATTACH_OPPOSITE_WIDGET,
			   RXmNtopWidget, alignwidget,
			   RXmNrightAttachment, RXmATTACH_FORM,
			   RXmNshowValue, false,
			   RXmNorientation, RXmHORIZONTAL,
			   RXmNbackground, get_color($sliderback)]);
end

def freq_callback(w, c, i)
  $freq = $low_freq + Rvalue(i) * (($high_freq - $low_freq) / 100.0);
  set_label(c, format("%8.3f", $freq));
end

def amp_callback(w, c, i)
  $amp = Rvalue(i) / 100.0;
  set_label(c, format("%8.3f", $amp));
end

def fm_callback(w, c, i)
  $index = Rvalue(i) * ($high_index / 100.0);
  set_label(c, format("%8.3f", $index));
end

def ratio_callback(w, c, i)
  $ratio = (Rvalue(i) * ($high_ratio / 100.0)).to_i;
  set_label(c, format("%8d", $ratio));
end

for i in 1 .. 15		# SIGHUP .. SIGTERM
  if(trap(i, "SIG_IGN") != 0)	# 0 for SIG_IGN
    trap(i) { |sig| 
      die("\nSignal #{sig} received.  Process canceled.", 2);
    }
  end
end

begin
  require "libxm";

  args = ([$0] << $*).flatten;
  $shell_app = RXtVaOpenApplication("FM", args.length, args,
				    RapplicationShellWidgetClass,
				    [RXmNallowShellResize, true,
				      RXmNtitle, "FM forever!"]);

  begin
    require "getoptlong";

    GetoptLong.new(["--play", "-p", GetoptLong::NO_ARGUMENT],
		   ["--frequency", "-f", GetoptLong::REQUIRED_ARGUMENT],
		   ["--index", "-i", GetoptLong::REQUIRED_ARGUMENT],
		   ["--ratio", "-r", GetoptLong::REQUIRED_ARGUMENT],
		   ["--amplitude", "-a", GetoptLong::REQUIRED_ARGUMENT],
		   ["--background", "-b", GetoptLong::REQUIRED_ARGUMENT],
		   ["--sliderback", "-s", GetoptLong::REQUIRED_ARGUMENT],
		   ["--version", "-V", GetoptLong::NO_ARGUMENT],
		   ["--help", "-h", "-?", GetoptLong::NO_ARGUMENT]).each { |name, arg|

      case name
      when "--play"
	$play = 1.0;
      when "--frequency"
	freq = arg.to_f.abs;
	$freq = freq < $low_freq ? $low_freq : 
	  freq > $high_freq ? $high_freq : freq;
      when "--index"
	ind = arg.to_f.abs;
	$index = ind > $high_index ? $high_index : ind;
      when "--ratio"
	rat = arg.to_i.abs;
	$ratio = rat > $high_ratio ? $high_ratio : rat;
      when "--amplitude"
	amp = arg.to_f.abs;
	$amp = amp > 1 ? 1 : amp;
      when "--sliderback"
	$sliderback = arg;
      when "--background"
	$background = arg;
      when "--version"
	$vers = true;
      when "--help"
	$help = true;
      end
    }
  rescue
    usage();
  end

  usage() if $help;
  version() if $vers;

  require "sndlib";

  app = $shell_app[1];
  shell = $shell_app[0];
  carosc = make_oscil(0.0);
  modosc = make_oscil(0.0);

  RXtAddEventHandler(shell, 0, true, lambda { |w, c, i, f| R_XEditResCheckMessages(w, c, i, f) });
  form = RXtCreateManagedWidget("form", RxmFormWidgetClass, shell,
				[RXmNresizePolicy, RXmRESIZE_GROW,
				  RXmNbackground, get_color($background)]);
  
  play_button = RXtCreateManagedWidget("Play", RxmToggleButtonWidgetClass, form,
				       [RXmNtopAttachment, RXmATTACH_FORM,
					 RXmNleftAttachment, RXmATTACH_FORM,
					 RXmNrightAttachment, RXmATTACH_NONE,
					 RXmNbottomAttachment, RXmATTACH_NONE,
					 RXmNbackground, get_color($background)]);

  RXmToggleButtonSetState(play_button, $play != 0.0 ? true : false, false);

  quit_button = RXtCreateManagedWidget(" Quit ", RxmPushButtonWidgetClass, form,
				       [RXmNtopAttachment, RXmATTACH_FORM,
					 RXmNleftAttachment, RXmATTACH_NONE,
					 RXmNrightAttachment, RXmATTACH_FORM,
					 RXmNbottomAttachment, RXmATTACH_NONE,
					 RXmNbackground, get_color($background)]);

  freq_label = make_scale_label(make_label("Carrier Frequency:", play_button, form), form);
  freq_scale = make_scale(freq_label, form);

  amp_label = make_scale_label(make_label("Amplitude:", freq_label, form), form);
  amp_scale = make_scale(amp_label, form);

  fm_label = make_scale_label(make_label("FM Index:", amp_label, form), form);
  fm_scale = make_scale(fm_label, form);

  cm_label = make_scale_label(make_label("C/M Ratio:", fm_label, form), form);
  cm_scale = make_scale(cm_label, form);

  RXtAddCallback(freq_scale, RXmNdragCallback, 
		 lambda { |w, c, i| freq_callback(w, c, i); }, freq_label);
  RXtAddCallback(freq_scale, RXmNvalueChangedCallback, 
		 lambda { |w, c, i| freq_callback(w, c, i); }, freq_label);

  RXtAddCallback(amp_scale, RXmNdragCallback, 
		 lambda { |w, c, i| amp_callback(w, c, i); }, amp_label);
  RXtAddCallback(amp_scale, RXmNvalueChangedCallback, 
		 lambda { |w, c, i| amp_callback(w, c, i); }, amp_label);

  RXtAddCallback(fm_scale, RXmNdragCallback, 
		 lambda { |w, c, i| fm_callback(w, c, i); }, fm_label);
  RXtAddCallback(fm_scale, RXmNvalueChangedCallback, 
		 lambda { |w, c, i| fm_callback(w, c, i); }, fm_label);

  RXtAddCallback(cm_scale, RXmNdragCallback, 
		 lambda { |w, c, i| ratio_callback(w, c, i); }, cm_label);
  RXtAddCallback(cm_scale, RXmNvalueChangedCallback, 
		 lambda { |w, c, i| ratio_callback(w, c, i); }, cm_label);

  RXtAddCallback(play_button, RXmNvalueChangedCallback, 
		 lambda { |w, c, i| $play = Rset(i) ? 1.0 : 0.0; });
  RXtAddCallback(quit_button, RXmNactivateCallback, 
		 lambda { |w, c, i| exit(0); });

  set_label(freq_label, format("%8.3f", $freq));
  set_label(amp_label, format("%8.3f", $amp));
  set_label(fm_label, format("%8.3f", $index));
  set_label(cm_label, format("%8d", $ratio));

  RXmScaleSetValue(freq_scale, (100 * ($freq - $low_freq) / ($high_freq - $low_freq)).to_i);
  RXmScaleSetValue(amp_scale, (100 * $amp).to_i);
  RXmScaleSetValue(fm_scale, (100 * $index / $high_index).to_i);
  RXmScaleSetValue(cm_scale, ($ratio * 100 / $high_ratio).to_i);

  bufsize = 256;
  srate = 22050;
  chans = 1;
  data = make_sound_data(chans, bufsize);
  port = mus_audio_open_output(Mus_audio_default, srate, chans, Mus_lshort, bufsize * 2);

  die("Can't open DAC!") if port < 0;

  RXtAppAddWorkProc(app, lambda { |dummy|
		      (0...bufsize).each { |i|
			sound_data_set!(data, 0, i, $amp * $play *
					oscil(carosc, hz2radians($freq) + $index * 
					      oscil(modosc, hz2radians($ratio * $freq))));
		      }
		      mus_audio_write(port, data, bufsize);
		      false;
		    });

  RXtRealizeWidget(shell);
  RXtAppMainLoop(app);
rescue
  die("RXtAppMainLoop()");
end

# bess.rb ends here

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us