«Sound file position selector GUI» by grirgz
on 23 Nov'14 20:47 inSometime you want to play a certain part of a sound file, but you must provide the Synthdef with the starting and ending buffer frame number. It's nice to be able to select it graphically. <Space> play the selected part (or till the end if no selection is made), <Enter> stop the playing. The position selected is posted on post window in the form [start, lenght, end]. The range slider allow zooming and scrolling.
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
( ~select_sample_position = { arg path; var player; var win, sf, sfview; var post_position; var vlayout; var zoom_slider; win = Window.new("select sample position", Rect(200, 300, 740, 100)); sf = SoundFile.new; sfview = SoundFileView.new; vlayout = VLayout.new; zoom_slider = RangeSlider.new; zoom_slider.orientation = \horizontal; zoom_slider.action = { arg sli; [\sliderLOW, sli.lo, \sliderHI, sli.hi].postln; sfview.zoomToFrac(sli.hi - sli.lo); sfview.scrollTo(sli.lo); }; vlayout.add(sfview); vlayout.add(zoom_slider); win.layout = vlayout; sf.openRead(path); sfview.soundfile = sf; sfview.read(0, sf.numFrames); sfview.elasticMode = true; sfview.timeCursorOn = true; sfview.timeCursorColor = Color.red; sfview.timeCursorPosition = 0; sfview.drawsWaveForm = true; sfview.gridOn = true; sfview.gridResolution = 1; post_position = { var cur; cur = sfview.selections[sfview.currentSelection]; "Current selection is now:\nframes: %\nseconds: %\nnormalized: %".format( cur++(cur[0]+cur[1]), cur++(cur[0]+cur[1]) / s.sampleRate, cur++(cur[0]+cur[1]) / sf.numFrames, ).postln; }; sfview.mouseUpAction = { arg a; post_position.(); }; sfview.keyDownAction = { arg view, char, modifiers, u, k; var cur; cur = view.selections[view.currentSelection]; //[char, modifiers, u, k].debug("KEYBOARD INPUT"); if( u == 32 ) { // space if(player.notNil) { player.stop; player = nil; }; player = sf.play( ( firstFrame: cur[0], lastFrame: if(cur[1] == 0) { nil } {cur[0]+cur[1]} ) ); post_position.(); }; if( u == 13 ) { // Enter if(player.notNil) { player.stop; player = nil; }; post_position.(); } }; win.view.keyDownAction = { arg view, char, modifiers, u, k; //[char, modifiers, u, k].debug("KEYBOARD INPUT"); if( u == 27 ) { // Esc if(player.notNil) { player.stop; player = nil; }; view.close(); }; }; win.front; }; // example ~select_sample_position.(Platform.resourceDir +/+ "sounds/a11wlk01.wav"); )
reception
This is cool! The only thing is I tried loading in different sound files and I kept getting error messages.