hlolli / csound-mode

Emacs major mode for coding in Csound
41 stars 14 forks source link

In Termux Emacs, csound-repl-start() does not find executable csound android app #6

Closed tj64 closed 5 years ago

tj64 commented 5 years ago

Hi, I would like to use csound-mode (with repl) in Spacemacs within Termux to edit and eval .csd files. Csound itself is neither in Termux nor in e.g. TermuxArch repos, so I cannot install it on the Linux side. But I did install the Csound App from Google Play Store, which works fine.

From inside Termux, I cannot access the csound executable (permission denied, phone is not rooted).

What I found so far is this hint for communication via UPD.

https://github.com/csound/csound/issues/865 The key lines to get things going: (setq CsoundProcess (make-network-process :name "Csound" :type 'datagram :buffer "Csound" :family 'ipv4 :host "127.0.0.1" :service "8099" :sentinel 'listen-sentinel :filter 'listen-filter))))

then... (process-send-string CsoundProcess (string-to-unibyte "csound code here")) I'm happy to share files, but I'm not sure how much time it would save you... it's part of a larger library. It's also a bit messy and undocumented. That

being said... I tried it in your csound-mode and it seemed to work OK. If you feel like a challenge I can email you direct or via the list. Just let me know.

Thorin.

Do you have any solution for this already? The only way I can think of at the moment is, in termux, put an executable .sh file in /usr/bin (exec path) that recieves the csound-repl calls (instead of the csound executable) and then forwards the calls via UPD to the csound Android app?

Cheers Thorsten

hlolli commented 5 years ago

I must say that I have literally no idea how the Android environment works. The only thing that the csound-repl expects is that csound is executeable from the path. If you can start emacs with an environmental variable like

PATH=$PATH:/path/to/bin/csound emacs -nw

to help it find the csound executeable, than you'd be in good hands. But the UDP communication which I use is purely automatic given that emacs can find the csound executeable.

tj64 commented 5 years ago

I investigated a bit more, starting out by making the hint cited above work on my Android 7 phone. All steps are (as well as the necessary csd and txt files) are described in https://github.com/csound/csound/issues/927. (note that in termux, my preinstalled "nc" command did not have a "-u" option (I think that was a 'nc java applet' or so), so I actually had to install netcat in termux and use it).

Then I had a look at csound-mode.el and Emacs network processes, and the hint cited above is the exact solution for the problem.

If you use the csound Android app and start the UDP server at port 8099 as describes in issue 927 cited above, and in termux/emacs put this in an emacs-lisp buffer (reusing the content from updtest.txt from issue 927), and evaluate the buffer, you should be able to hear that annoying tone.

`(setq CsoundProcess (make-network-process :name "Csound" :type 'datagram :buffer "Csound" 
:family 'ipv4 :host "127.0.0.1" :service "8099" :sentinel 'listen-sentinel :filter 'listen-filter))

(process-send-string CsoundProcess (string-to-unibyte 
                                              "instr 1
                                                prints \"hello world\"
                                                a1 vco2 p5,p5 
                                                outs a1 , a1
                                                endin
                                                schedule 1,0,3, 0.2, 440"))`

So it's actually the same approach as in csound-repl.el, and only one problem remains: when I call csound-repl-start I get "Csound not installed on your computer" instead of a working repl.

To fix this, csound-repl--start-server should become an optional call, depending on a user prompt, maybe something like this:

`[ ] start local csound server yes-or-no?`

Then, when in termux, I could opt for no and use the csound Android app as local csound udp server.

Cheers Thorsten

hlolli commented 5 years ago

Ok, you are totally right. It should be possible to just start listenig on a udp port without starting Csound. Provided with port, one can of course start Csound somewhere else. There's much work I need to do with the repl that I haven't got around to. It compiles instruments and sends events, but I hope to add more tooling.

Would you be ok with an interface like M-x csound-repl-listen RET < port > etc?

tj64 commented 5 years ago

I forked the repo and started to hack around a little, its not that trivial like I thought, but should be doable. I ended up adding a new boolean custom csound-repl-start-server-p in csound-repl that I then use in all places where the server subprocess is called/referenced. That way I could start a REPL, i.e. I see a REPL buffer, but nothing works yet.

One important thing seems to be to keep comint happy, besides not providing a server process. This has already be done before (by providing a fake process) and could be copied: https://www.emacswiki.org/emacs/ComintModes. And maybe with such fake process, there is no need anymore for the somehow hackish use of csound-repl-start-server-p conditionals in many places?

This weekend and I can dig further, so maybe you just wait for me for a first sketch of a possible solution? But it would of course not be a problem if you come first with a better solution than me ;-)

It would be really nice to have the full REPL functionality already available in csound-mode in Termux/Emacs, just have a slightly different communication style in the background.

tj64 commented 5 years ago

Sucessfully closed with PR https://github.com/hlolli/csound-mode/pull/7 Thanks