1
const ToneStream = require('tone-stream')                                                                            
const Speaker = require('speaker');                                                                                  
const blessed = require('blessed');                                                                                  
const format = {                                                                                                     
  sampleRate: 8000,                                                                                                  
  bitDepth: 16,                                                                                                      
  channels: 1                                                                                                        
}                                                                                                                    

let stream = new ToneStream(format);                                                                                 
const speaker = new Speaker(format);                                                                                 

stream.pipe(speaker);                                                                                                

var screen = blessed.screen({                                                                                        
  smartCSR: true                                                                                                     
});                                                                                                                  

let boxarray = [];                                                                                                   

for(let i = 0; i < 10; i++) {                                                                                        
  let box = blessed.box({                                                                                            
     top: "0%",                                                                                                      
     left: (i/10 *100).toString() + '%',                                                                             
     width: (1/11 *100).toString() + '%',                                                                            
     height: "100%",                                                                                                 
     tags: true,                                                                                                     
     style: {                                                                                                        
       bg: "white"                                                                                                   
     }                                                                                                               
  });                                                                                                                
  box.on('click', () => {                                                                                            
    stream.add([4000, 440]);                                                                                         
    console.log(i);                                                                                                  
  });                                                                                                                
  boxarray.push(box);                                                                                                
}                                                                                                                    

boxarray.forEach((box) => {                                                                                          
    screen.append(box);                                                                                              
});                                                                                                                  

screen.key(['escape', 'q', 'C-c'], function(ch, key) {                                                               
  return process.exit(0);                                                                                            
});                                                                                                                  

screen.render(); 

I'm trying to get a tone to play when each UI box is clicked. My onclick event added to the boxes isn't working. The console.log works but no tone is played. How can I get a tone to play when a ui box is clicked? The reference to stream still exists, I can console log it without receiving null. The stream is also still open.

GameKyuubi
  • 591
  • 1
  • 6
  • 17
  • 1
    What's wrong with the code you wrote? Also, if this is node.js how are you getting clicks? – Jared Smith Jun 08 '20 at 02:01
  • I'm using blessed.js for a curses-style interface. "fen" in this case is actually blessed. Perhaps I should be a bit more explicit with what I'm doing. The problem with this code is that foo.add(i) doesn't do anything. I can console log it but foo is not in scope when baz is clicked later. – GameKyuubi Jun 08 '20 at 02:07
  • Are you keeping reference to `baz` somewhere else? For each iteration, `baz` is re-assigned and then you'll lose the reference to `baz`, hence `onclick` on that old `baz` is doing nothing. – Duc Nguyen Jun 08 '20 at 02:29
  • In my implementation, the onclick works if I put a console.log in it. I click baz and the log happens. But if I also try to do something with foo, it doesn't work. – GameKyuubi Jun 08 '20 at 02:33
  • Please show a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). This code does NOT have the problem you claim. `foo` is in scope and if it has a `.add()` method, it will get called each time the `click` event is triggered on `baz`. So, something else is doing on that is not shown in this code. – jfriend00 Jun 08 '20 at 02:42
  • My first guess based on seeing `foo.pipe(bar)` is that `foo` is a stream that is now closed and thus `foo.add(i)` doesn't do anything on a closed stream when the click occurs sometime later. But, as I said above, please show us a complete working and reproducible example. – jfriend00 Jun 08 '20 at 02:43
  • Ok, I have reworked my example. Sorry for the confusion. – GameKyuubi Jun 08 '20 at 03:07
  • 1
    So much better when you show the REAL code rather than your pseudo-code. I don't know why people here tend to think their question should be pseudo-code, not real code. We can always help more directly and accurately with the real code. Now that you have posted the real code, I was able to suggest an answer. – jfriend00 Jun 08 '20 at 03:37
  • Do you have any other suggestions? – GameKyuubi Jun 08 '20 at 16:13

0 Answers0