KoltesDigital / atom-foxdot

FoxDot interface for Atom
10 stars 3 forks source link

Display responses to print statements #8

Closed sandcobainer closed 4 years ago

sandcobainer commented 4 years ago

Displaying print statements isn't supported by the Logger service. This might be easy to develop looking at your npm repository. What do you think? Is it possible to get responses from the Python thread being spawned?

print(SynthDefs) or print(P[0, 1, 2, (3, 4, 5)]) could be useful before executing.

KoltesDigital commented 4 years ago

"It's working on my machine!"

image

Would you mind debugging to understand why it's not working for you?

sandcobainer commented 4 years ago

Just tried it again off a fresh copy of your master branch, but doesn't help. Probably because I use MacOS. Here's a screenshot Screen Shot 2020-05-04 at 12 56 13 PM

sandcobainer commented 4 years ago

Added a console.log(data) to the childProcess, but nothing gets logged on the Chrome console too. this.childProcess.stdout.on('data', (data) => { console.log(data); logger === null || logger === void 0 ? void 0 : logger.stdout(data); });

KoltesDigital commented 4 years ago

Weird. Can you please try to run FoxDot from CLI with --pipe, type print(something) enter enter, see what it does? Maybe there's an env var somewhere preventing logs to be displayed.

sandcobainer commented 4 years ago

Yep, I've tried this. Sorry forgot to mention that. python -m FoxDot --pipe print(SynthDefs) ['sawbass', 'karp', 'gong', 'varsaw', 'bell', 'feel', 'scratch', 'pulse', 'audioin', 'blip', 'pads', 'rave', 'donk', 'saw', 'orient', 'creep', 'growl', 'marimba', 'razz', 'stretch', 'dub', 'pasha', 'keys', 'jbass', 'arpy', 'zap', 'viola', 'quin', 'ambi', 'dbass', 'crunch', 'noise', 'star', 'bass', 'dab', 'dirt', 'twang', 'swell', 'pluck', 'glass', 'soprano', 'charm', 'spark', 'bug', 'squish', 'sitar', 'snick', 'play2', 'play1', 'sinepad', 'prophet', 'ripple', 'space', 'fuzz', 'lazer', 'klank', 'nylon', 'soft', 'scatter', 'loop']

it looks like something to do with child_process.spawn

KoltesDigital commented 4 years ago

What happens if you write in a new file const cp = child_process.spawn... and cp.stdout.on('data'..., and run it directly with nodejs?

sandcobainer commented 4 years ago

I ran this in a JS file with 2012s-mbp:test a2012$ node test.js 2 the process exited with no data, the 2 is from on close.


let command = ['-m', 'FoxDot', '--pipe', 'print(SynthDefs)']
childProcess = child_process_1.spawn('python', command);
childProcess.stdout.on('data', (data) => {
     console.log(data);
});
childProcess.on('close', (code) => {
    console.log(code);
});```
KoltesDigital commented 4 years ago

You shouldn't put print(SynthDefs) into the CLI arguments, it won't do anything, instead you should do

childProcess.stdin.write('print(SynthDefs)\n\n');
childProcess.stdin.end();
KoltesDigital commented 4 years ago

BTW non-zero exit code is for error, so you should add childProcess.stderr.on('data', (data) => { console.error(data); }); and maybe even childProcess.on('error', (err) => { console.error(err); }); (which misses in my code as well).

sandcobainer commented 4 years ago

Sorry about that, I fixed it. with stdin.write and and error handling and I could get something out of the stdout.

<Buffer 5b 27 73 61 77 62 61 73 73 27 2c 20 27 6b 61 72 70 27 2c 20 27 67 6f 6e 67 27 2c 20 27 76 61 72 73 61 77 27 2c 20 27 62 65 6c 6c 27 2c 20 27 66 65 65 ... 485 more bytes>

So it looks like it's working ok with node, just something up within the repository?

KoltesDigital commented 4 years ago

FTR this buffer translates to ['sawbass'..., I should add .toString() here.

Can you please add the following in the Atom code, and see what it says?

this.childProcess.stdout.on('end', () => {
    console.log('STDOUT END');
});
this.childProcess.stderr.on('end', () => {
    console.log('STDERR END');
});
this.childProcess.on('error', (err) => {
    console.log('ERROR');
    console.log(err);
});
sandcobainer commented 4 years ago

Adding this to foxdot.js also didn't help. I was able to get the array of instruments using .toString() though. Could it be an issue with my node setup?

sandcobainer commented 4 years ago

I removed everything and installed a fresh copy of atom-foxdot from the Atom package manager directly just to be sure. And still have the same problem. FYI, Everything else in the package runs fine.

KoltesDigital commented 4 years ago

Interesting. Do you mean, changing line 18 in lib/foxdot.js from

logger === null || logger === void 0 ? void 0 : logger.stdout(data);

to

logger === null || logger === void 0 ? void 0 : logger.stdout(data.toString());

makes it work?

sandcobainer commented 4 years ago

Sorry, I meant I got it to work by running test.js directly with Node.

const child_process_1 = require("child_process");

let command = ['-m', 'FoxDot', '--pipe']

childProcess = child_process_1.spawn('python', command);
childProcess.stdin.write('print(SynthDefs)\n\n');
childProcess.stdin.end();

childProcess.stdout.on('data', (data) => {
     console.log(data.toString());
});

childProcess.stderr.on('error', (err) => {
    console.log(err);
});

The repository still doesn't display any data or throw any error either.

KoltesDigital commented 4 years ago

Dumb questions but just to be sure:

  1. apart from this problem, does the plugin work, i.e. can you make any sound?
  2. have you set the plugin's python path to something?

I really can't see... I'll ask around if other Mac users can have a look at it.

sandcobainer commented 4 years ago

Everything else works. I can evaluate, get audio from supercollider, clear clock and all. Only print() silently fails

sandcobainer commented 4 years ago

Hmm, any update on this? I'm sorry I couldn't help debug this. I'm surprised there's no error or exception thrown in the console. It might be my node/atom setup or something silly that I'm doing. Just to be sure, I'm using Python3. I might switch to Python2 and see what happens.

KoltesDigital commented 4 years ago

No news from other Mac users 😕

By any chance, can you please add console.log(process.env) in the plugin code, and see what it prints? Especially if you have other plugins like PlatformIO which interfer with the python environment.

Alternatively, you may try plugins for other editors. The truth is I prefer VS Code, I'm wondering why I made the plugin for Atom haha. But I haven't tried the VSC plugin yet and I don't know what features it has.

sandcobainer commented 4 years ago

I see, I'm just using Atom as it has an API of hackable functions, but this issue is dragging me down. Were you looking for something specific from this process.env property path:

ATOM_DISABLE_SHELLING_OUT_FOR_ENVIRONMENT: "true"
ATOM_HOME: "/Users/a2012/.atom"
Apple_PubSub_Socket_Render: "/private/tmp/com.apple.launchd.AtHB03A8ft/Render"
DISPLAY: "/private/tmp/com.apple.launchd.aEwloUpDZC/org.macosforge.xquartz:0"
HOME: "/Users/a2012"
LANG: "en_US.UTF-8"
LOGNAME: "a2012"
NODE_ENV: "production"
NODE_PATH: "/Applications/Atom.app/Contents/Resources/app.asar/exports"
NVM_CD_FLAGS: ""
NVM_DIR: "/Users/a2012/.nvm"
NVM_RC_VERSION: ""
PATH: "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin"
PWD: "/Users/a2012/.atom/packages/evoMusic-foxdot"
SHELL: "/bin/bash"
SHLVL: "2"
SSH_AUTH_SOCK: "/private/tmp/com.apple.launchd.vZuyPxDaqP/Listeners"
TERM: "xterm-256color"
TERM_PROGRAM: "Apple_Terminal"
TERM_PROGRAM_VERSION: "421.2"
TERM_SESSION_ID: "5E0B6FD3-5A1A-4CAC-87A0-3ACF5EDEB8AB"
TMPDIR: "/var/folders/73/qd5n1bcx215_s5l37tlf0zx40000gn/T/"
USER: "a2012"
XPC_FLAGS: "0x0"
XPC_SERVICE_NAME: "com.github.atom.16600.B3C3CA9A-32BE-4C6D-B0F0-D30BF21196BE"
_: "/usr/bin/open"
__CF_USER_TEXT_ENCODING: "0x1F5:0x0:0x0"
sandcobainer commented 4 years ago

@KoltesDigital here's how to managed to fix it on MacOS, not sure if this breaks the Windows version, so I'm not gonna ask you to edit the code base, but i just had to add a -u flag to the Python command. so let command = ['-u', '-m', 'FoxDot', '--pipe']; and all print and error messages are back on the logger.

KoltesDigital commented 4 years ago

Wow good finding! Just to be curious, which version of Python do you have? The documentation states it's the default since version 3.7, and mine is 3.7.3. Also doc says setting PYTHONUNBUFFERED (e.g. in ~/.bash_profile) would do the same, so that you wouldn't need to patch the plugin.

sandcobainer commented 4 years ago

Ah, that makes sense. I'm using 3.6.5. I will just upgrade it to 3.8. That should fix it. You don't have to change any of the code.

I'm working a customized Atom plugin for a generative code editor (reason to use Atom over VS Code). Thank you for helping me debug this!