A
download draw.rb
Language: Ruby
LOC: 383
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# draw.rb -- draw.scm --> draw.rb -*- snd-ruby -*-

# Translator: Michael Scholz <scholz-micha@gmx.de>
# Created: Tue Apr 05 00:17:04 CEST 2005
# Changed: Sat Nov 19 16:39:52 CET 2005

# Commentary:
#
# examples of extensions to Snd's graphics
# 
# module Draw
#  display_colored_samples(color, beg, dur, snd = false, chn = false)
#  display_samples_in_color(snd, chn)
#  color_samples(color, beg = 0, dur = false, snd = Snd.snd, chn = Snd.chn)
#  uncolor_samples(snd = Snd.snd, chn = Snd.chn)
#  display_previous_edits(snd, chn)
#  overlay_sounds(*rest)
#  samples_via_colormap(snd, chn)
#
#  update_current_window_location(snd)
#  display_current_window_location(snd, chn)
#  click_current_window_location(snd, chn, button, state, x, y, axis)
#  make_current_window_display
#  close_current_window_display
#  
#  smart_line_cursor(snd, chn, ax)
#  click_for_listener_help(pos)
#  

# Code:

require "examp"
require "extensions"

module Draw
  add_help(:display_colored_samples,
           "display_colored_samples(color, beg, dur, [snd=false, [chn=false]])  \
displays samples from beg for dur in color whenever they\'re in the current view.")
  def display_colored_samples(color, beg, dur, snd = false, chn = false)
    unless array?(color) and number?(beg) and number?(dur)
      return
    end
    left = left_sample(snd, chn)
    right = right_sample(snd, chn)
    len = beg + dur
    old_color = foreground_color(snd, chn)
    if left < len and right > beg
      if vct?(data = make_graph_data(snd, chn))
        samps = [right, len].min - [left, beg].max
        offset = [0, beg - left].max
        new_data = data.subseq(offset, offset + samps)
        set_foreground_color(color, snd, chn)
        graph_data(new_data, snd, chn, Copy_context, [beg, left].max, [len, right].min)
        set_foreground_color(old_color, snd, chn)
      else
        low_data, high_data = data[0, 2]
        size = low_data.length
        samps = right - left
        left_offset = [0, beg - left].max
        left_bin = ((size.to_f * left_offset) / samps).floor
        right_offset = [len, right].min - left
        right_bin = ((size.to_f * right_offset) / samps).floor
        new_low_data = low_data.subseq(left_bin, right_bin)
        new_high_data = high_data.subseq(left_bin, right_bin)
        set_foreground_color(color, snd, chn)
        graph_data([new_low_data, new_high_data], snd, chn, Copy_context, left_bin, right_bin)
        set_foreground_color(old_color, snd, chn)
      end
    end
  end

  def display_samples_in_color(snd, chn)
    col, beg, dur = channel_property(:colored_samples, snd, chn)
    display_colored_samples(col, beg, dur, snd, chn)
  end

  add_help(:color_samples,
           "color_samples(color, [beg=0, [dur=false, [snd=false, [chn=false]]]])  \
causes samples from beg to beg+dur to be displayed in color")
  def color_samples(color, beg = 0, dur = false, snd = Snd.snd, chn = Snd.chn)
    unless $after_graph_hook.member?("display-samples-in-color")
      $after_graph_hook.add_hook!("display-samples-in-color") do |snd, chn|
        display_samples_in_color(snd, chn)
      end
    end
    unless dur then dur = frames(snd, chn) - beg end
    set_channel_property(:colored_samples, [color, beg, dur], snd, chn)
    update_time_graph(snd, chn)
  end

  add_help(:uncolor_samples,
           "uncolor_samples([snd=false, [chn=false]]) cancels sample coloring in the given channel")
  def uncolor_samples(snd = Snd.snd, chn = Snd.chn)
    set_channel_property(:colored_samples, [], snd, chn)
    update_time_graph(snd, chn)
  end

  add_help(:display_previous_edits,
           "display_previous_edits(snd, chn)  \
displays all edits of the current sound, with older versions gradually fading away")
  def display_previous_edits(snd, chn)
    edits = edit_position(snd, chn)
    old_color = foreground_color(snd, chn)
    clist = color2list(old_color)
    r = clist[0]
    g = clist[1]
    b = clist[2]
    rinc = (1.0 - r) / (edits + 1)
    ginc = (1.0 - g) / (edits + 1)
    binc = (1.0 - b) / (edits + 1)
    if edits > 0
      re = 1.0 - rinc
      ge = 1.0 - ginc
      be = 1.0 - binc
      0.upto(edits) do |pos|
        data = make_graph_data(snd, chn, pos)
        set_foreground_color(make_color(re, ge, be), snd, chn)
        graph_data(data, snd, chn)
        re -= rinc
        ge -= ginc
        be -= binc
      end
      set_foreground_color(old_color, snd, chn)
    end
  end

  add_help(:overlay_sounds,
           "overlay_sounds(*rest)  \
overlays onto its first argument all subsequent arguments: overlay_sounds(1, 0, 3)")
  def overlay_sounds(*rest)
    base = rest.shift
    $after_graph_hook.add_hook!(get_func_name) do |snd, chn|
      if sound?(base) and snd == base
        rest.each do |snd|
          if sound?(snd) and channels(snd) > chn and channels(base) > chn
            graph_data(make_graph_data(snd, chn), base, chn, Copy_context, -1, -1, Graph_dots)
          end
        end
      end
    end
  end

  add_help(:samples_via_colormap,
           "samples_via_colormap(snd, chn)  \
displays time domain graph using current colormap (just an example of colormap-ref)")
  def samples_via_colormap(snd, chn)
    left = left_sample(snd, chn)
    right = right_sample(snd, chn)
    old_color = foreground_color(snd, chn)
    data, ignore = make_graph_data(snd, chn)
    samps = right - left
    x0 = x2position(left / srate())
    y0 = y2position(data[0])
    colors = make_array(colormap_size)
    j = 1
    (left + 1).upto(left + data.length - 1) do |i|
      x1 = x2position(i / srate)
      y1 = y2position(data[j])
      x = data[j].abs
      ref = (colormap_size * x).floor
      unless colors[ref]
        colors[ref] = make_color(*colormap_ref(colormap, x))
      end
      set_foreground_color(colors[ref], snd, chn)
      draw_line(x0, y0, x1, y1)
      x0, y0 = x1, y1
      j += 1
    end
    set_foreground_color(old_color, snd, chn)
  end

  # inset overall waveform; if click, move to that location
  $current_window_display_is_running = false # for prefs
  
  Inset_width = 0.2
  Inset_height = 0.25
  
  def update_current_window_location(snd)
    channels(snd).times do |chn|
      if vals = channel_property(:inset_envelope, snd, chn)
        vals[:edit_position] = -2 # set edit_position to impossible value
      end
    end
    false
  end
  
  def display_current_window_location(snd, chn)
    if time_graph?(snd, chn)
      axinf = axis_info(snd, chn)
      grf_width = axinf[12]
      width = (Inset_width * Float(grf_width)).round
      x_offset = (grf_width - width).to_i
      grf_height = axinf[11] - axinf[13]
      height = (Inset_height * Float(grf_height)).round
      chan_offset = axinf[13] - 10
      y_offset = chan_offset + (height * 0.5).round
      grf_chn = ((channel_style(snd) == Channels_separate) ? chn : 0)
      new_peaks = axinf[19]
      if width > 10 and height > 10 and frames(snd, chn) > 0 and
          (chn == 0 or channel_style(snd) != Channels_superimposed)
        fill_rectangle(x_offset, chan_offset + height, width, 2, snd, grf_chn)
        fill_rectangle(x_offset, chan_offset, 2, height, snd, grf_chn)
        frms = frames(snd, chn).to_f
        rx = (width * (right_sample(snd, chn) / frms)).round
        lx = (width * (left_sample(snd, chn) / frms)).round
        fill_rectangle(x_offset + lx,
                       chan_offset,
                       [1, rx - lx].max,
                       height,
                       snd,
                       grf_chn,
                       Selection_context)
        if (old_env = channel_property(:inset_envelope, snd, chn)) and
            (not new_peaks) and
            width == old_env[:width] and
            height == old_env[:height] and
            y_offset == old_env[:y_offset] and
            edit_position(snd, chn) == old_env[:edit_position]
          data0 = old_env[:data0]
          data1 = old_env[:data1]
        else
          data = make_graph_data(snd, chn, Current_edit_position, 0, frames(snd, chn))
          data_max = (vct?(data) ? data.peak : data.map do |v| v.peak end.max)
          data_scaler = ((data_max > 0.0) ? (height / (2.0 * data_max)) : 0.0)
          new_len = width * 2
          data_len = (vct?(data) ? data.length : data.car.length)
          step = (data_len / width.to_f).to_i
          if data_len > width
            data0 = make_array(new_len)
            data1 = ((not vct?(data)) and make_array(new_len))
            i = 0
            j = 0
            max_y = -data_max
            min_y = data_max
            stepper = 0.0
            until data_len == i or new_len == j
              if data1
                max_y = [max_y, data.cadr[i]].max
                min_y = [min_y, data.car[i]].min
              else
                max_y = [max_y, data[i]].max
              end
              stepper += 1.0
              if stepper >= step
                data0[j] = x_offset
                data0[j + 1] = (y_offset - max_y * Float(data_scaler)).round
                max_y = -data_max
                if data1
                  data1[j] = x_offset
                  data1[j + 1] = (y_offset - min_y * Float(data_scaler)).round
                  min_y = data_max
                end
                x_offset += 1
                stepper -= step
                j += 2
              end
              i += 1
            end
            while j < new_len
              data0[j] = data0[j - 2]
              data0[j + 1] = data0[j - 1]
              if data1
                data1[j] = data1[j - 2]
                data1[j + 1] = data1[j - 1]
              end
              j += 2
            end
          else
            xstep = (width.to_f / data_len).to_i
            data0 = make_array(data_len * 2)
            data1 = ((not vct?(data)) and make_array(data_len * 2))
            j = 0
            xj = x_offset
            data_len.times do |i|
              data0[j] = xj.round
              if data1
                data0[j + 1] = (y_offset - data.cadr[i] * Float(data_scaler)).round
                data1[j] = xj.floor
                data1[j + 1] = (y_offset - data.car[i] * Float(data_scaler)).round
              else
                data0[j + 1] = (y_offset - data[i] * Float(data_scaler)).round
              end
              j += 2
              xj += xstep
            end
          end
          set_channel_property(:inset_envelope,
                               {:width, width,
                                 :height, height,
                                 :edit_position, edit_position(snd, chn),
                                 :data0, data0,
                                 :data1, data1,
                                 :y_offset, y_offset},
                               snd, chn)
        end
        draw_lines(data0, snd, grf_chn)
        data1 and draw_lines(data1, snd, grf_chn)
      end
    end
  end
  
  def click_current_window_location(snd, chn, button, state, x, y, axis)
    if axis == Time_graph
      axinf = axis_info(snd, chn)
      grf_width = axinf[12]
      width = (Inset_width * Float(grf_width)).round
      x_offset = (grf_width - width).to_i
      grf_height = axinf[11] - axinf[13]
      height = (Inset_height * Float(grf_height)).round
      chan_offset = axinf[13] - 10
      y_offset = chan_offset + (height * 0.5).round
      if width > 0 and
          x >= x_offset and
          x <= grf_width and
          y >= chan_offset and
          y <= (chan_offset + height)
        samp = (Float(frames(snd, chn)) * ((x - Float(x_offset)) / width)).round
        ls = left_sample(snd, chn)
        rs = right_sample(snd, chn)
        set_cursor(samp, snd, chn)
        if (samp < ls) or (samp > rs)
          rsamp = [[0, samp - (0.5 * (ls - rs)).round].max, frames(snd, chn) - 1].min
          set_right_sample(rsamp, snd, chn)
        end
        update_time_graph(snd, chn)
        # FIXME:
        # Is smpte the reason for the need of the repetition of
        # fill_rectangles (update_time_graph() -> $after_graph_hook)?
        grf_chn = ((channel_style(snd) == Channels_separate) ? chn : 0)
        fill_rectangle(x_offset, chan_offset + height, width, 2, snd, grf_chn)
        fill_rectangle(x_offset, chan_offset, 2, height, snd, grf_chn)
        frms = frames(snd, chn).to_f
        rx = (width * (right_sample(snd, chn) / frms)).round
        lx = (width * (left_sample(snd, chn) / frms)).round
        fill_rectangle(x_offset + lx,
                       chan_offset,
                       [1, rx - lx].max,
                       height,
                       snd,
                       grf_chn,
                       Selection_context)
        true
      else
        false
      end
    else
      false
    end
  end

  CWD_name = "current-window-display"
  
  add_help(:make_current_window_display,
           "make_current_window_display()  \
Display in upper right corner the overall current sound and where the current window fits in it.")
  def make_current_window_display
    unless $current_window_display_is_running
      $after_open_hook.add_hook!(CWD_name) do |snd|
        channels(snd).times do |chn|
          set_channel_property_save_state_ignore(:inset_envelope, snd, chn)
          undo_hook(snd, chn).add_hook!(CWD_name + chn.to_s) do | |
            if vals = channel_property(:inset_envelope, snd, chn)
              vals[:edit_position] = -2 # set edit_position to impossible value
            end
          end
        end
      end
      if RUBY_VERSION >= "1.9.0"
        $after_graph_hook.add_hook!(CWD_name, &method(:display_current_window_location).to_proc)
        $mouse_click_hook.add_hook!(CWD_name, &method(:click_current_window_location).to_proc)
        $update_hook.add_hook!(CWD_name, &method(:update_current_window_location).to_proc)
      else
        $after_graph_hook.add_hook!(CWD_name) do |snd, chn|
          display_current_window_location(snd, chn)
        end
        $mouse_click_hook.add_hook!(CWD_name) do |snd, chn, button, state, x, y, axis|
          click_current_window_location(snd, chn, button, state, x, y, axis)
        end
        $update_hook.add_hook!(CWD_name) do |snd|
          update_current_window_location(snd)
        end
      end
      $current_window_display_is_running = true
    end
  end

  def close_current_window_display
    if $current_window_display_is_running
      $after_open_hook.remove_hook!(CWD_name)
      $after_graph_hook.remove_hook!(CWD_name)
      $mouse_click_hook.remove_hook!(CWD_name)
      $update_hook.remove_hook!(CWD_name)
      Snd.sounds.each do |snd|
        channels(snd).times do |chn|
          undo_hook(snd, chn).remove_hook!(CWD_name + chn.to_s)
        end
      end
      $current_window_display_is_running = false
    end
  end

  add_help(:smart_line_cursor,
           "smart_line_cursor(snd, chn, ax)  \
is a cursor_style function that tries not to overwrite the thumbnail graph \
in the upper right corner.")
  def smart_line_cursor(snd, chn, ax)
    x, y = cursor_position
    x0, y0, x1, y1 = axis_info(snd, chn, ax)[10, 4]
    inset_x0 = x1 * (1.0 - Inset_width)
    inset_y0 = (y1 - 10.0) + (Inset_height * (y0 - y1))
    if x > (inset_x0 - 5)
      draw_line(x, y0, x, inset_y0 + 5, snd, chn, Cursor_context)
    else
      draw_line(x, y0, x, y1 - 5, snd, chn, Cursor_context)
    end
  end

  # click-for-listener-help

  $last_click_time = 0.0

  def click_for_listener_help(pos)
    time = Time.now.to_f
    if time - $last_click_time < 0.2
      $last_click_time = 0.0
      if string?(text = widget_text(main_widgets[4]))
        if string?(subject = text.slice(text.rindex(/\b/m, pos)...text.index(/\b/m, pos)))
          if string?(help = snd_help(subject, false))
            help_dialog(subject, help)
          end
        end
      end
    else
      $last_click_time = time
    end
  end
  # $listener_click_hook.add_hook!("listener-help", &method(:click_for_listener_help).to_proc)
end

include Draw

# draw.rb ends here

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