A
download gtk-popup.scm
Language: Scheme
LOC: 733
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
;;; translated from popup.scm
;;;
;;; no attempt here to set widget colors, no listener popup menu (gtk predefines
;;;   a popup for each entry/textview widget and there's no way to get rid of it!
;;;   perhaps we could find a pointer to its children and hide all of them?)

(use-modules (ice-9 format))
(define popup-already-loaded (provided? 'snd-gtk-popup.scm))
(provide 'snd-gtk-popup.scm)

(if (not (provided? 'xg))
    (let ((hxm (dlopen "xg.so")))
      (if (string? hxm)
	  (snd-error (format #f "gtk-popup.scm needs the xg module: ~A" hxm))
	  (dlinit hxm "Init_libxg"))))

(define (change-label w new-label)
  "(change-label widget new-label) changes widget's label to be new-label"
  (if w
      (if (GTK_IS_LABEL w)
	  (gtk_label_set_text (GTK_LABEL w) new-label)
	  (gtk_label_set_text (GTK_LABEL (gtk_bin_get_child (GTK_BIN w))) new-label))))

(define* (g_signal_connect obj name func #:optional data)
  (g_signal_connect_closure_by_id (GPOINTER obj)
				  (g_signal_lookup name (G_OBJECT_TYPE (GTK_OBJECT obj)))
				  0
				  (g_cclosure_new func data #f)
				  #f))

(define (make-popup-menu top-field-func entries)
  "(make-popup-menu top-field-func entries) creates a popup menu"
  (let ((menu (gtk_menu_new)))
    (if top-field-func
	(top-field-func menu))
    (for-each
     (lambda (entry)
       ;; entry is list: name field-func optional-callback
       (let ((widget (if (car entry)
			 (gtk_menu_item_new_with_label (car entry))
			 (gtk_menu_item_new)))) ; separator
	 (gtk_menu_shell_append (GTK_MENU_SHELL menu) widget)
	 (if (and (cadr entry) 
		  (procedure? (cadr entry)))
	     ((cadr entry) widget))
	 (gtk_widget_show widget)
	 (if (and (not (null? (cddr entry)))
		  (procedure? (caddr entry)))
	     (g_signal_connect widget "activate" (lambda (w data) 
						   ((caddr entry) w data) 
						   (gtk_widget_hide menu)) #f))))
     entries)
    menu))


(define (display-properties props)
  ;; there's no way to tell Guile's format that enormous vectors should not be printed in full
  ;; so we search for them here and handle them ourselves

  (define (vector-print v)
    (if (< (vector-length v) 3)
	(object->string v)
	(let ((str (format #f "'#(~A" (vector-ref v 0))))
	  (do ((i 1 (1+ i)))
	      ((= i 3))
	    (set! str (string-append str " " (object->string (vector-ref v i)))))
	  (string-append str " ...)"))))

  (let ((str ""))
    (for-each
     (lambda (pr)
       (let ((property (car pr))
	     (value (cdr pr)))
	 (set! str (string-append str (string #\newline) "    " (object->string property) ": "))
	 (if (vector? value)
	     (set! str (string-append str (vector-print value)))
	     (if (list? value)
		 (let ((prev #f))
		   (set! str (string-append str "'("))
		   (for-each
		    (lambda (v)
		      (if prev 
			  (set! str (string-append str " "))
			  (set! prev #t))
		      (if (vector? v)
			  (set! str (string-append str (vector-print v)))
			  (set! str (string-append str (object->string v)))))
		    value)
		   (set! str (string-append str ")")))
		 (set! str (string-append str (object->string value)))))))
     props)
    str))


(if (not popup-already-loaded)
    (let* ((selection-popup-menu 
	    ;; used in graph if pointer is inside selected portion
	    (let ((every-menu (lambda (w) #f)) ; local settings
		  (stopping #f)
		  (stopping1 #f)
		  (stop-widget #f)
		  (stop-widget1 #f))
	      ;; -------- selection popup
	      (make-popup-menu 
	       (lambda (w) ; top level settings
		 #f)
	       (list
		;; name field-func callback
		(list "Selection" #f)
		(list #f #f) ; separator
		(list "Play" every-menu
		      (lambda (w data)
			(if stopping
			    (begin
			      (set! stopping #f)
			      (change-label w "Play")
			      (if stopping1
				  (begin
				    (set! stopping1 #f)
				    (change-label stop-widget1 "Loop play")))
			      (stop-playing)) ; stops all including possible looping play
			    (begin
			      (change-label w "Stop")
			      (set! stop-widget w)
			      (set! stopping #t)
			      (play-selection #f (lambda (reason)
						   (set! stopping #f)
						   (change-label w "Play")))))))
		(list "Loop play" every-menu
		      (lambda (w data) 
			(define (stop-playing-selection)
			  (set! stopping1 #f)
			  (change-label w "Loop play")
			  (if stopping
			      (begin
				(set! stopping #f)
				(change-label stop-widget "Play"))))
			(define (play-selection-again reason)
			  (if (and (not (c-g?))
				   (= reason 0)
				   stopping1)
			      (play-selection #f play-selection-again)
			      (stop-playing-selection)))
			(if stopping1
			    (begin
			      (stop-playing-selection)
			      (stop-playing))
			    (begin
			      (change-label w "Stop!")
			      (set! stop-widget1 w) ; needs to be separate from Play case since we're stopping/restarting deliberately
			      (set! stopping1 #t)
			      (play-selection #f play-selection-again)))))
		(list "Delete"    every-menu (lambda (w data) (delete-selection)))
		(list "Zero"      every-menu (lambda (w data) (scale-selection-by 0.0)))
		(list "Crop"      every-menu
		      (lambda (w data)
			;; delete everything except selection
			(for-each
			 (lambda (selection)
			   (as-one-edit
			    (lambda ()
			      (let* ((snd (car selection))
				     (chn (cadr selection))
				     (beg (selection-position snd chn))
				     (len (selection-frames snd chn)))
				(if (> beg 0) 
				    (delete-samples 0 beg snd chn))
				(if (< len (frames snd chn))
				    (delete-samples (+ len 1) (- (frames snd chn) len) snd chn))))))
			 (let ((sndlist '()))
			   (for-each (lambda (snd)
				       (do ((i (1- (channels snd)) (1- i)))
					   ((< i 0))
					 (if (selection-member? snd i)
					     (set! sndlist (cons (list snd i) sndlist)))))
				     (sounds))
			   sndlist))))
		(list "Save as"   every-menu (lambda (w data) (save-selection-dialog)))
		(list "Copy->New" every-menu 
		      (lambda (w data) 
			(let ((new-file-name (snd-tempnam)))
			  (save-selection new-file-name)
			  (open-sound new-file-name))))
		(list "Cut->New"   every-menu 
		      (lambda (w data) 
			(let ((new-file-name (snd-tempnam)))
			  (save-selection new-file-name)
			  (delete-selection)
			  (open-sound new-file-name))))
		(list "Snap marks" every-menu 
		      (lambda (w data)
			(for-each 
			 (lambda (select)
			   (let ((pos  (apply selection-position select))
				 (len  (apply selection-frames select)))
			     (apply add-mark pos select)
			     (apply add-mark (+ pos len) select)))
			 (selection-members))))
		(list "Apply controls" every-menu (lambda (w data) (apply-controls (selected-sound) 2))) ; 2=selection
		(list "Reset controls" every-menu (lambda (w data) (reset-controls)))
		(list "Unselect"       every-menu (lambda (w data) (set! (selection-member? #t) #f)))
		(list "Reverse"        every-menu (lambda (w data) (reverse-selection)))
		(list "Mix"            every-menu (lambda (w data) (mix-selection (cursor))))
		(list "Invert"         every-menu (lambda (w data) (scale-selection-by -1)))))))
	   
	   ;; -------- time domain popup
	   
	   (graph-popup-snd #f)
	   (graph-popup-chn #f)
	   
	   (save-popup-menu #f)
	   (undo-popup-menu #f)
	   (revert-popup-menu #f)
	   (play-previous-popup-menu #f)
	   (play-channel-popup-menu #f)
	   (redo-popup-menu #f)
	   (mix-selection-popup-menu #f)
	   (play-cursor-popup-menu #f)
	   (unselect-popup-menu #f)
	   (insert-selection-popup-menu #f)
	   (replace-with-selection-popup-menu #f)
	   (delete-mark-popup-menu #f)
	   (next-mark-popup-menu #f)
	   (last-mark-popup-menu #f)
	   (play-original-popup-menu #f)
	   
	   (graph-popup-menu 
	    ;; used within graph if pointer is not inside selected portion
	    (let ((every-menu (lambda (w) #f))
		  (stopping #f)
		  (stop-widget #f))
	      
	      (add-hook! stop-playing-hook
			 (lambda (snd) 
			   (if stopping
			       (begin
				 (set! stopping #f)
				 (if (GTK_IS_WIDGET stop-widget)
				     (change-label stop-widget "Play"))))))
	      
	      (make-popup-menu 
	       (lambda (menu) #f)
	       (list
		(list "Play" every-menu 
		      (lambda (w data) 
			(if stopping
			    (begin
			      (set! stopping #f)
			      (change-label w "Play")
			      (stop-playing))
			    (begin
			      (change-label w "Stop")
			      (set! stopping #t)
			      (play 0 graph-popup-snd))))
		      (lambda (wid)
			(set! stop-widget wid)))
		(list "Play channel" 
		      (lambda (w) (set! play-channel-popup-menu w))
		      (lambda (w data)
			(set! stopping #t)
			(change-label stop-widget "Stop")
			(play 0 graph-popup-snd graph-popup-chn)))
		(list "Play from cursor"
		      (lambda (w) (set! play-cursor-popup-menu w))
		      (lambda (w data)
			(set! stopping #t)
			(change-label stop-widget "Stop")
			(play (cursor graph-popup-snd graph-popup-chn) graph-popup-snd)))
		(list "Play previous"
		      (lambda (w) (set! play-previous-popup-menu w))
		      (lambda (w data)
			(set! stopping #t)
			(change-label stop-widget "Stop")
			(play 0 graph-popup-snd graph-popup-chn #f #f (1- (edit-position)))))  ; play version before most-recent edit
		(list "Play original"
		      (lambda (w) (set! play-original-popup-menu w))
		      (lambda (w data)
			(set! stopping #t)
			(change-label stop-widget "Stop")
			(play 0 graph-popup-snd graph-popup-chn #f #f 0)))                     ; play unedited version
		(list "Undo"
		      (lambda (w) (set! undo-popup-menu w))
		      (lambda (w data)
			(undo 1 graph-popup-snd graph-popup-chn)))
		(list "Redo"
		      (lambda (w) (set! redo-popup-menu w))
		      (lambda (w data)
			(redo 1 graph-popup-snd graph-popup-chn)))
		(list "Revert"
		      (lambda (w) (set! revert-popup-menu w))
		      (lambda (w data)
			(revert-sound graph-popup-snd)))
		(list "Save"
		      (lambda (w) (set! save-popup-menu w))
		      (lambda (w data)
			(save-sound graph-popup-snd)))
		(list "Save as"             every-menu 
		      (lambda (w data)
			(select-sound graph-popup-snd)
			(save-sound-dialog)))
		(list "Close"               every-menu 
		      (lambda (w data)
			(close-sound graph-popup-snd)))
		(list "Mix selection"
		      (lambda (w) (set! mix-selection-popup-menu w))
		      (lambda (w data)
			(mix-selection (cursor graph-popup-snd graph-popup-chn) graph-popup-snd graph-popup-chn)))
		(list "Insert selection"
		      (lambda (w) (set! insert-selection-popup-menu w))
		      (lambda (w data)
			(insert-selection (cursor graph-popup-snd graph-popup-chn) graph-popup-snd graph-popup-chn)))
		(list "Replace with selection"
		      (lambda (w) (set! replace-with-selection-popup-menu w))
		      (lambda (w data)
			(let* ((snd graph-popup-snd)
			       (chn graph-popup-chn)
			       (beg (cursor snd chn))
			       (len (selection-frames))
			       (sbeg (selection-position)))
			  (if (or (not (selection-member? snd chn))
				  (< (+ beg len) sbeg)
				  (> beg (+ sbeg len)))
			      (begin
				(delete-samples beg len snd chn)
				(insert-selection beg snd chn))
			      (if (< beg sbeg)
				  (delete-samples beg (- sbeg beg) snd chn)
					;(snd-warning "replace at ~D would collide with selected portion")
				  )))))
		(list "Select all"          every-menu 
		      (lambda (w data)
			(select-all graph-popup-snd graph-popup-chn)))
		(list "Unselect"
		      (lambda (w) (set! unselect-popup-menu w))
		      (lambda (w data) 
			(set! (selection-member? #t) #f)))
		(list "Apply controls"          every-menu 
		      (lambda (w data) 
			(apply-controls)))
		(list "Reset controls"          every-menu 
		      (lambda (w data) 
			(reset-controls)))
		(list "Info"                every-menu 
		      (lambda (w data)
			(let ((snd graph-popup-snd))
			  (info-dialog 
			   (format #f "~A info" (file-name snd))
			   (format #f "~A:~%  chans: ~D~%  srate: ~D~%  header: ~A~%  data format: ~A~%  length: ~1,3F~%  maxamp: ~A~%~A~A~A~A~A"
				   (short-file-name snd)
				   (chans snd)
				   (srate snd)
				   (mus-header-type-name (header-type snd))
				   (mus-data-format-name (data-format snd))
				   (exact->inexact (/ (frames snd graph-popup-chn) (srate snd)))
				   (maxamp snd #t)
				   (if (comment snd)
				       (format #f "  comment: \"~A\"~%" (comment snd))
				       "")
				   (let ((loops (mus-sound-loop-info (file-name snd))))
				     (if (not (null? loops))
					 (format #f "  loop: ~A~%" loops)
					 ""))
				   (if (= (header-type snd) mus-soundfont)
				       (format #f "  sounds: ~:{~%     ~S start: ~A, loop: ~A ~A~}" (soundfont-info))
				       "")
				   (if (not (null? (sound-properties snd)))
				       (format #f "  properties: ~A"
					       (display-properties (sound-properties snd)))
				       "")
				   (let ((chan-str ""))
				     (do ((i 0 (1+ i)))
					 ((= i (chans snd)))
				       (if (not (null? (channel-properties snd i)))
					   (set! chan-str 
						 (string-append chan-str
								(format #f "~%  chan ~D properties: ~A" 
									i (display-properties (channel-properties snd i)))))))
				     chan-str)
				   )))))
		(list "Add mark"            every-menu 
		      (lambda (w data)
			(add-mark (cursor graph-popup-snd graph-popup-chn) graph-popup-snd graph-popup-chn)))
		(list "Delete mark"
		      (lambda (w) (set! delete-mark-popup-menu w))
		      (lambda (w data)
			;; find mark closest to cursor and delete it
			(let* ((ms (marks graph-popup-snd graph-popup-chn))
			       (id (if (null? ms)
				       #f
				       (if (= (length ms) 1)
					   (car ms)
					   (let ((loc (cursor)))
					     (define (find-closest-mark lst cur-min cur-id)
					       (if (null? lst)
						   cur-id
						   (let* ((this-id (car lst))
							  (this-min (abs (- loc (mark-sample this-id)))))
						     (if (< this-min cur-min)
							 (find-closest-mark (cdr lst) this-min this-id)
							 (find-closest-mark (cdr lst) cur-min cur-id)))))
					     (find-closest-mark (cdr ms) 
								(abs (- loc (mark-sample (car ms)))) 
								(car ms)))))))
			  (if id
			      (delete-mark id)))))
		(list "To next mark"
		      (lambda (w) (set! next-mark-popup-menu w))
		      (lambda (w data)
			(key (char->integer #\j) 4 graph-popup-snd graph-popup-chn)))
		(list "To last mark"
		      (lambda (w) (set! last-mark-popup-menu w))
		      (lambda (w data)
			(key (char->integer #\-) 4 graph-popup-snd graph-popup-chn)
			(key (char->integer #\j) 4 graph-popup-snd graph-popup-chn)))
		(list #f #f) ; separator
		(list "Exit"                every-menu 
		      (lambda (w data)
			(exit)))
		))))
	   
	   ;; -------- fft popup (easier to access than Options:Transform)
	   
	   (peaks-popup-menu #f)
	   (db-popup-menu #f)
	   (log-freq-popup-menu #f)
	   (fft-sizes '())
	   (fft-types '())
	   (wavelet-types '())
	   (fft-norms '())
	   (fft-windows '())
	   (norms (list graph-once graph-as-sonogram graph-as-spectrogram))
	   (sizes (list 16 64 256 1024 4096 16384 65536 262144 1048576))
	   (windows (list rectangular-window hann-window welch-window parzen-window bartlett-window hamming-window blackman2-window 
			  blackman3-window blackman4-window exponential-window riemann-window kaiser-window cauchy-window 
			  poisson-window gaussian-window tukey-window dolph-chebyshev-window hann-poisson-window connes-window
			  samaraki-window ultraspherical-window))
	   (types (list fourier-transform wavelet-transform autocorrelation cepstrum walsh-transform haar-transform))
	   
	   (fft-popup-menu #f)
	   ;; used within graph if pointer is in the fft graph
	   (fft-popup-menu-1 ; needed since fft-popup-menu is accessed in this context
	    (let* ((every-menu (lambda (w) #f)))
	      (define (choose-chan)
		(if (= (channel-style graph-popup-snd) channels-separate) graph-popup-chn #t))
	      
	      (define (make-simple-popdown-menu top popdown-labels label-list)
		(let* ((top-cascade (gtk_menu_new)))
		  (gtk_menu_item_set_submenu (GTK_MENU_ITEM top) top-cascade)
		  (for-each (lambda (poplab)
			      (let ((child (gtk_menu_item_new_with_label (car poplab))))
				(gtk_menu_shell_append (GTK_MENU_SHELL top-cascade) child)
				(gtk_widget_show child)
				(if (eq? label-list 'fft-sizes)
				    (set! fft-sizes (cons child fft-sizes))
				    (if (eq? label-list 'fft-types)
					(set! fft-types (cons child fft-types))
					(if (eq? label-list 'fft-windows)
					    (set! fft-windows (cons child fft-windows))
					    (if (eq? label-list 'fft-norms)
						(set! fft-norms (cons child fft-norms))
						(set! wavelet-types (cons child wavelet-types))))))
				(g_signal_connect child "activate" (lambda (w data) ((cadr poplab) w data) (gtk_widget_hide fft-popup-menu)))))
			    popdown-labels)))
	      
	      (make-popup-menu 
	       (lambda (menu) #f)
	       (list
		(list "Peaks" 
		      (lambda (w)
			(set! peaks-popup-menu w))
		      (lambda (w data)
			(set! (show-transform-peaks graph-popup-snd (choose-chan)) (not (show-transform-peaks graph-popup-snd graph-popup-chn)))))
		(list "dB" 
		      (lambda (w)
			(set! db-popup-menu w))
		      (lambda (w data)
			(set! (fft-log-magnitude graph-popup-snd (choose-chan)) (not (fft-log-magnitude graph-popup-snd graph-popup-chn)))))
		(list "Log freq"
		      (lambda (w)
			(set! log-freq-popup-menu w))
		      (lambda (w data)
			(set! (fft-log-frequency graph-popup-snd (choose-chan)) (not (fft-log-frequency graph-popup-snd graph-popup-chn)))))
		(list "Normalize" every-menu
		      (lambda (w data)
			(if (= (transform-normalization graph-popup-snd graph-popup-chn) dont-normalize)
			    (set! (transform-normalization graph-popup-snd (choose-chan)) normalize-by-channel)
			    (set! (transform-normalization graph-popup-snd (choose-chan)) dont-normalize))))
		(list "Graph type"
		      (lambda (w)
			(make-simple-popdown-menu 
			 w
			 (map (lambda (name val)
				(list name
				      (lambda (w data) 
					(set! (transform-graph-type graph-popup-snd (choose-chan)) val))))
			      (list "once" "sonogram" "spectrogram")
			      norms)
			 'fft-norms)))
		(list "Size"
		      (lambda (w)
			(make-simple-popdown-menu 
			 w
			 (map (lambda (name val)
				(list name
				      (lambda (w data) 
					(set! (transform-size graph-popup-snd (choose-chan)) val))))
			      (map (lambda (n) (number->string n)) sizes)
			      sizes)
			 'fft-sizes)))
		(list "Window"
		      (lambda (w)
			(make-simple-popdown-menu
			 w
			 (map (lambda (name val)
				(list name
				      (lambda (w data) 
					(set! (fft-window graph-popup-snd (choose-chan)) val))))
			      (list "Rectangular" "Hann" "Welch" "Parzen" "Bartlett" "Hamming" "Blackman2" "Blackman3" "Blackman4"
				    "Exponential" "Riemann" "Kaiser" "Cauchy" "Poisson" "Gaussian" "Tukey" "Dolph-Chebyshev" "Hann-Poisson" 
				    "Connes" "Samaraki" "Ultraspherical")
			      windows)
			 'fft-windows)))
		(list "Transform type"
		      (lambda (w)
			(make-simple-popdown-menu 
			 w
			 (map (lambda (name val)
				(list name 
				      (lambda (w data)
					(set! (transform-type graph-popup-snd (choose-chan)) val))))
			      (list "Fourier" "Wavelet" "Autocorrelate" "Cepstrum" "Walsh" "Haar")
			      types)
			 'fft-types)))
		(list "Wavelet type"
		      (lambda (w)
			(make-simple-popdown-menu 
			 w
			 (let ((ctr -1))
			   (map (lambda (name)
				  (set! ctr (+ ctr 1))
				  (list name 
					(lambda (w data)
					  (set! (wavelet-type graph-popup-snd (choose-chan)) ctr))))
				(list "daub4" "daub6" "daub8" "daub10" "daub12" "daub14" "daub16" "daub18" "daub20" "battle_lemarie" 
				      "burt_adelson" "beylkin" "coif2" "coif4" "coif6" "sym2" "sym3" "sym4" "sym5" "sym6")))
			 'wavelet-types)))
		(list "Color" every-menu
		      (lambda (w data) (color-dialog)))
		(list "Orientation" every-menu
		      (lambda (w data) (orientation-dialog))))))))
      (set! fft-types (reverse fft-types))
      (set! fft-windows (reverse fft-windows))
      (set! fft-norms (reverse fft-norms))
      (set! fft-sizes (reverse fft-sizes))
      (set! wavelet-types (reverse wavelet-types))
      (set! fft-popup-menu fft-popup-menu-1)
      (add-hook! gtk-popup-hook
		 (lambda (widget event data snd chn)
		   (let* ((e (GDK_EVENT_BUTTON event))
			  (x (.x e))
			  (y (.y e))
			  (menu graph-popup-menu))
		     (if snd
			 (begin
			   (set! graph-popup-snd snd)
			   (set! graph-popup-chn chn)
			   (if (= (channel-style snd) channels-combined)
			       (call-with-current-continuation
				(lambda (break)
				  (do ((i 0 (1+ i)))
				      ((= i (chans snd)))
				    (let ((off (list-ref (axis-info snd i) 14)))
				      (if (< y off)
					  (begin
					    (set! graph-popup-chn (- i 1))
					    (break)))))
				  (set! graph-popup-chn (- (chans snd) 1)))))
			   (let ((fax (if (transform-graph? snd chn) (axis-info snd chn transform-graph) #f))
				 (lax (if (lisp-graph? snd chn) (axis-info snd chn lisp-graph) #f)))
			     (if (and fax
				      (>= x (list-ref fax 10))
				      (<= x (list-ref fax 12)))
				 ;; in fft
				 (begin
				   (change-label peaks-popup-menu (if (show-transform-peaks snd chn) "No peaks" "Peaks"))
				   (change-label db-popup-menu (if (fft-log-magnitude snd chn) "Linear" "dB"))
				   (change-label log-freq-popup-menu (if (fft-log-frequency snd chn) "Linear freq" "Log freq"))
				   (for-each (lambda (child size)
					       (gtk_widget_set_sensitive child (not (= (transform-size graph-popup-snd graph-popup-chn) size))))
					     fft-sizes sizes)
				   (for-each (lambda (child norm)
					       (gtk_widget_set_sensitive child (not (= (transform-graph-type graph-popup-snd graph-popup-chn) norm))))
					     fft-norms norms)
				   (for-each (lambda (child window)
					       (gtk_widget_set_sensitive child (not (= (fft-window graph-popup-snd graph-popup-chn) window))))
					     fft-windows windows)
				   (for-each (lambda (child type)
					       (gtk_widget_set_sensitive child (not (= (transform-type graph-popup-snd graph-popup-chn) type))))
					     fft-types types)
				   (let ((ctr 0))
				     (for-each (lambda (child)
						 (gtk_widget_set_sensitive child (not (= (wavelet-type graph-popup-snd graph-popup-chn) ctr)))
						 (set! ctr (1+ ctr)))
					       wavelet-types))
				   (set! menu fft-popup-menu))
				 (if (and lax
					  (>= x (list-ref lax 10))
					  (<= x (list-ref lax 12)))
				     ;; in lisp
				     ;;   nothing special implemented yet
				     ;; (set! menu graph-popup-menu)
				     #f ; just a place-holder
				     
				     (if (and (selection?)
					      (let* ((beg (/ (selection-position snd graph-popup-chn) (srate snd)))
						     (end (/ (+ (selection-position snd graph-popup-chn) 
								(selection-frames snd graph-popup-chn)) 
							     (srate snd))))
						(and (>= x (x->position beg snd chn))
						     (<= x (x->position end snd chn)))))
					 (set! menu selection-popup-menu)
					 (let* ((eds (edits graph-popup-snd graph-popup-chn))
						(with-edit (if (> (car eds) 0) gtk_widget_show gtk_widget_hide))
						(nchans (chans graph-popup-snd))
						(with-selection (if (selection?) gtk_widget_show gtk_widget_hide))
						(with-marks (if (not (null? (marks graph-popup-snd graph-popup-chn))) gtk_widget_show gtk_widget_hide)))
					   (set! menu graph-popup-menu)
					   (with-edit save-popup-menu)
					   (with-edit undo-popup-menu)
					   (with-edit revert-popup-menu)
					   (with-edit play-previous-popup-menu)
					   ((if (> (cadr eds) 0) gtk_widget_show gtk_widget_hide) redo-popup-menu)
					   ((if (> nchans 1) gtk_widget_show gtk_widget_hide) play-channel-popup-menu)
					   (with-selection mix-selection-popup-menu)
					   (with-selection replace-with-selection-popup-menu)
					   (with-selection insert-selection-popup-menu)
					   (with-selection unselect-popup-menu)
					   ((if (> (cursor graph-popup-snd graph-popup-chn) 0) gtk_widget_show gtk_widget_hide) play-cursor-popup-menu)
					   ((if (> (car eds) 1) gtk_widget_show gtk_widget_hide) play-original-popup-menu)
					   (with-marks delete-mark-popup-menu)
					   (with-marks next-mark-popup-menu)
					   (with-marks last-mark-popup-menu))))))
			   (gtk_widget_show menu)
			   (gtk_menu_popup (GTK_MENU menu) #f #f #f #f (.button e) (.time e))
			   #t)
			 #f))))))


;;; -------- edit history popup

(define edhist-funcs '())
(define edhist-widgets '())
(define edhist-channel-widgets '())
(define edhist-snd #f)
(define edhist-chn #f)
(define edhist-time 0)

(define (edhist-clear-edits)
  (set! edhist-funcs '())
  #f)

(define (edhist-save-edits)
  (let* ((old-val (assoc (cons edhist-snd edhist-chn) edhist-funcs))
	 (cur-edits (edits edhist-snd edhist-chn))
	 (new-func (edit-list->function edhist-snd edhist-chn (1+ (car cur-edits)) (apply + cur-edits))))
    (if old-val
	(set-cdr! old-val new-func)
	;; perhaps this should save the previous function under the current file name?
	(set! edhist-funcs (cons (cons (cons edhist-snd edhist-chn) new-func) edhist-funcs)))
    #f))

(define (edhist-reapply-edits)
  (let* ((old-val (assoc (cons edhist-snd edhist-chn) edhist-funcs)))
    (if old-val	((cdr old-val) edhist-snd edhist-chn))))

(define (edhist-apply-edits)
  ;; the current popdown widgets are in edhist-widgets, current funcs are in edhist-funcs
  ;; edhist-apply (for new widgets) is car of edhist-widgets

  (define (edhist-apply w d)
    (let ((index (cadr (g_object_get_data (G_OBJECT w) "popup-index"))))
      (if (and (> index 0)
	       (<= index (length edhist-funcs)))
	  ((cdr (list-ref edhist-funcs (1- index))) edhist-snd edhist-chn))))
	  
  (let ((wids (cdr edhist-widgets))
	(funcs edhist-funcs)
	(parent (car edhist-widgets))
	(index 1))
    (if (not (null? funcs))
	(for-each
	 (lambda (func)
	   (let* ((label (car func))
		  (button (if (not (null? wids))
			      (let ((wid (car wids)))
				(set! wids (cdr wids))
				wid)
			      (let ((wid (gtk_menu_item_new_with_label "label")))
				(gtk_menu_shell_append (GTK_MENU_SHELL parent) wid)
				(set! edhist-widgets (append edhist-widgets (list wid)))
				(g_signal_connect wid "activate" edhist-apply)
				wid))))
	     (if (pair? label)
		 (change-label button (format #f "~A[~A]" (short-file-name (car label)) (cdr label)))
		 (change-label button label))
	     (g_object_set_data (G_OBJECT button) "popup-index" (GPOINTER index))
	     (gtk_widget_show button)
	     (set! index (1+ index))))
	 edhist-funcs))
    (if (not (null? wids))
	(for-each
	 (lambda (w)
	   (gtk_widget_hide w)))
	 wids)
    (gtk_menu_popup (GTK_MENU parent) #f #f #f #f 3 edhist-time)
    #f))

(define (edhist-help-edits)
  (help-dialog "Edit History Functions"
	       "This popup menu gives access to the edit-list function handlers in Snd. \
At any time you can backup in the edit list, 'save' the current trailing edits, make some \
new set of edits, then 'reapply' the saved edits.  The 'apply' choice gives access to all \
currently saved edit lists -- any such list can be applied to any channel.  'Clear' deletes \
all saved edit lists."
	       (list "{edit lists}" "{edit-list->function}")
	       (list "extsnd.html#editlists" "extsnd.html#editlist_to_function")
	       ))

(define edhist-save-menu #f)
(define edhist-apply-menu #f)
(define edhist-reapply-menu #f)
(define edhist-clear-menu #f)

;(gc) -- trouble here?
(define edit-history-menu
  (let ((every-menu (lambda (w) #f)))
    (make-popup-menu 
     (lambda (w) #f)
     (list
      (list "Edits"    #f)
      (list #f #f)
      (list "Save"
	    (lambda (w) (set! edhist-save-menu w))
	    (lambda (w d) (edhist-save-edits)))
      (list "Reapply"
	    (lambda (w) (set! edhist-reapply-menu w))
	    (lambda (w d) (edhist-reapply-edits)))
      (list "Apply"
	    (lambda (w)
	      (set! edhist-apply-menu w)
	      (let* ((top-cascade (gtk_menu_new)))
		(gtk_menu_item_set_submenu (GTK_MENU_ITEM w) top-cascade)
		(set! edhist-widgets (list top-cascade))))
	    (lambda (w d) (edhist-apply-edits)))
      (list "Clear"
	    (lambda (w) (set! edhist-clear-menu w))
	    (lambda (w d) (edhist-clear-edits)))
      (list "Help"     #f 
	    (lambda (w d) (edhist-help-edits)))))))

(define (add-edhist-popup snd)
  (let ((chns (chans snd)))
    (do ((i 0 (1+ i)))
	((= i chns))
      (let ((edhist (list-ref (channel-widgets snd i) 7)))
	(if (not (member edhist edhist-channel-widgets))
	    (begin
	      (set! edhist-channel-widgets (cons edhist edhist-channel-widgets))
	      (g_signal_connect edhist "button_press_event" 
		(lambda (w e d) 
		  (let ((button (.button (GDK_EVENT_BUTTON e))))
		    (if (= button 3)
			(begin
			  (set! edhist-snd snd)
			  (set! edhist-chn i)
			  (gtk_widget_set_sensitive edhist-clear-menu (not (null? edhist-funcs)))
			  (gtk_widget_set_sensitive edhist-save-menu (> (apply + (edits snd i)) 0))
			  (gtk_widget_set_sensitive edhist-apply-menu (not (null? edhist-funcs)))
			  (gtk_widget_set_sensitive edhist-reapply-menu (not (eq? #f (assoc (cons snd i) edhist-funcs))))
			  (gtk_widget_show edit-history-menu)
			  (set! edhist-time (.time (GDK_EVENT_BUTTON e)))
			  (gtk_menu_popup (GTK_MENU edit-history-menu) #f #f #f #f (.button (GDK_EVENT_BUTTON e)) (.time (GDK_EVENT_BUTTON e)))
			  #t) ; don't select anything in the list (i.e. don't pass event to widget)
			#f)))
		#f)
	      ))))))

(add-hook! after-open-hook add-edhist-popup)
(add-hook! close-hook (lambda (snd)
			(let ((chns (chans snd))
			      (name (short-file-name snd)))
			  (do ((i 0 (1+ i)))
			      ((= i chns))
			    (let* ((old-val (assoc (cons snd i) edhist-funcs)))
			      (if old-val
				  (set-car! old-val (format #f "~A[~A]" name i))))))))
(for-each add-edhist-popup (sounds))


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