// title: Sound file position selector GUI // author: grirgz // description: // Sometime 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. play the selected part (or till the end if no selection is made), stop the playing. The position selected is posted on post window in the form [start, lenght, end]. The range slider allow zooming and scrolling. // code: ( ~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"); )