Search Unity

Csound and Unity..

Discussion in 'Audio & Video' started by rorywalsh, May 19, 2016.

  1. rorywalsh

    rorywalsh

    Joined:
    Apr 10, 2015
    Posts:
    114
    If anyone is interested, I've written a Csound wrapper for Unity. Needless to say it makes it very easy to do all sorts of procedural music, and gives users all of Csound's synthesis and audio processing tools, including ambisonic decoding/encoding, bin-aurual diffusion, OSC support, etc. It's completely open source and available on my github page:

    http://rorywalsh.github.io/CsoundUnity/

    Let me know if you have any questions, suggestions, or comments.

    Rory.

    p.s. I'm relatively new here, I hope I'm not breaking forum etiquette by posting this announcement...
     
    Last edited: May 19, 2016
    videon, twobob, mtytel and 1 other person like this.
  2. aihodge

    aihodge

    Joined:
    Nov 23, 2014
    Posts:
    163
    Very cool, will definitely try this out. Thanks for sharing!
     
  3. rorywalsh

    rorywalsh

    Joined:
    Apr 10, 2015
    Posts:
    114
    Let me know if you come up with anything interesting. I'm pretty new to Unity but I'm loving learning to use it. Throw in Csound and I'm in heaven.
     
  4. laggyluk

    laggyluk

    Joined:
    Oct 7, 2011
    Posts:
    32
    Hey, nice work
    Have you any experience running it on mobile? I'm working on sort of sequencer and sound synthesis would be great addition to it. Unfortunately most of system resources is spent on drawing the UI and not much left for anything else..
     
  5. rorywalsh

    rorywalsh

    Joined:
    Apr 10, 2015
    Posts:
    114
    I haven't had time to explore this yet, but I've used Csound for Android applications before, so technically I don't see why it wouldn't work. Lots of people have also used Csound on iOS. It would involve a bit of work. It's something I would like to add when I get the time.
     
  6. maladie

    maladie

    Joined:
    Jul 3, 2016
    Posts:
    3
    Thanks for this. I've been a long time Csound user but relatively new to Unity. Does anyone know if something of the kind exists for MaxMSP and/or Pure Data as well since I'm more comfortable in those environments when making procedural music.
     
  7. rorywalsh

    rorywalsh

    Joined:
    Apr 10, 2015
    Posts:
    114
    You can try this for Pure Data. I never got it to work, and I think you may need to compile libpd yourself which is a pain. Considering that you have to code in both environments, i.e., Csound and Unity, I find the two go together quite well.
     
  8. g__b

    g__b

    Joined:
    Sep 17, 2014
    Posts:
    39
    I cannot make it work. I tried all the three scenes included in the package.
    Unity hangs forever after hitting play.
    I'm on Os X 10.9.5, Unity 5.3.5f1, and I have Csound 6.07 installed.
     
  9. rorywalsh

    rorywalsh

    Joined:
    Apr 10, 2015
    Posts:
    114
    You don't actually need to have Csound installed but that shouldn't be a problem. I have only tried this with 10.10. Did you try the scenes before installing Csound? I'm not in a position look into this right now as I'm taking a few weeks off. How are your C# chops? You could try editing the code so that Unity loads your installed Csound over the one that ships with the package. Or you can try using Csound with fmod
    http://csound.github.io/site/news/2016/07/15/fmod_and_csound
    Sorry I can't offer more support right now. Just bad timing I'm afraid but the fmod plugin should work ok.
     
  10. g__b

    g__b

    Joined:
    Sep 17, 2014
    Posts:
    39
    Thank you for your answer. I will try it on Os X 10.10, then.
    No hurry!
     
  11. g__b

    g__b

    Joined:
    Sep 17, 2014
    Posts:
    39
    Tried on Os X 10.10, same problem, Unity hangs forever and I have to force quit.
    If I add a CsoundUnity component to a gameobject without specifying the csd file, I have the following output:

    I also tried to load Csound from my installed one, setting dataPath="/Library/Frameworks"; inside CsoundUnity.cs
    No error, but Unity hangs if I add a csd file and hit play.
     
  12. g__b

    g__b

    Joined:
    Sep 17, 2014
    Posts:
    39
    Now it's working (also without changing datapath)! I don't know why, I just restarted Unity several times.
     
  13. rorywalsh

    rorywalsh

    Joined:
    Apr 10, 2015
    Posts:
    114
    Glad it's working but obviously totally miffed as to why. Perhaps Csound was already running as a process?
     
  14. g__b

    g__b

    Joined:
    Sep 17, 2014
    Posts:
    39
    I think not. I didn't use Csound with CsoundQt or anything similar, nor via CLI. But shouldn't it be able to run multiple instances simultaneously?
    I have to say I also tried reinstalling Csound 6.07 a couple of times.
    Then, after some Unity hanging/restarting, it worked.
    Now I am going to try some csd to test it!
    Thank you for your work, I was looking for this since ages!
     
  15. rorywalsh

    rorywalsh

    Joined:
    Apr 10, 2015
    Posts:
    114
    Let me know how it goes for you. I'd be happy to answer any specific questions you have. The good thing is that you can go ahead and add your own methods or change things as you see fit. The source is not that complex at all.
     
  16. g__b

    g__b

    Joined:
    Sep 17, 2014
    Posts:
    39
    I tried to build a standalone app for Os X, and I get this error:
    I had to (brutally) add some compiler flags to export a standalone build:

    CsoundUnity.cs, line 20:
    Code (CSharp):
    1. #if UNITY_EDITOR
    2. using UnityEditor;
    3. #endif
    CsoundUnity.cs, line 211:
    Code (CSharp):
    1. /**
    2.     * process a ksmps-sized block of samples
    3.     */
    4.     public string getFilePath(Object obj)
    5.     {
    6.         #if UNITY_EDITOR
    7.         return Application.dataPath.Replace("Assets", "") + AssetDatabase.GetAssetPath(obj);
    8.         #endif
    9.         return "";
    10.     }
     
  17. rorywalsh

    rorywalsh

    Joined:
    Apr 10, 2015
    Posts:
    114
    Yes, the current code in the repo is missing this. I have more up to date code but I've not had a chance to update yet. It'll have to wait till I am back at work as it's right there on my work PC. But as you might imagine, I'm in no rush to get back to work ;)
     
    g__b likes this.
  18. g__b

    g__b

    Joined:
    Sep 17, 2014
    Posts:
    39
    As I said, no hurry! I am very happy with what I have right now, a lot to experiment with :)
    Thank you again!
     
  19. g__b

    g__b

    Joined:
    Sep 17, 2014
    Posts:
    39
    just a little bump to ask if I can expect an update..
    I have audio only on left channel, what can I try?
     
  20. rorywalsh

    rorywalsh

    Joined:
    Apr 10, 2015
    Posts:
    114
    Can you confirm you have the latest source? I can look into this tomorrow as I'll have a bit of time then.
     
  21. rorywalsh

    rorywalsh

    Joined:
    Apr 10, 2015
    Posts:
    114
    I just tried here with the latest source and it works as expected in stereo. Can you make sure that the files in your CsoundUnityScripts folder are up to date with this found here:
    https://github.com/rorywalsh/CsoundUnity/tree/master/CsoundUnityScripts
    Especially the CsoundUnity.cs file. I pushed a solution to this problem to the repo on May the 28th.
     
  22. g__b

    g__b

    Joined:
    Sep 17, 2014
    Posts:
    39
    I am trying with the latest source, and now Unity is hanging again..
    there are multiple warnings in the Editor log like:

    WARNING: [mnot loading 'libbuchla.dylib' (incompatible with this version of Csound (3.1/3.0)[m

    and a final Segmentation Fault

    WARNING: [mnot loading 'libbuchla.dylib' (incompatible with this version of Csound (3.1/3.0)[m
    [mWARNING: [mnot loading 'libcellular.dylib' (incompatible with this version of Csound (3.1/3.0)[m
    [mWARNING: [mnot loading 'libcmidi.dylib' (incompatible with this version of Csound (3.1/3.0)[m
    [mWARNING: [mnot loading 'libcontrol.dylib' (incompatible with this version of Csound (3.1/3.0)[m
    [mWARNING: [mnot loading 'libcs_date.dylib' (incompatible with this version of Csound (3.1/3.0)[m
    [mWARNING: [mnot loading 'libexciter.dylib' (incompatible with this version of Csound (3.1/3.0)[m
    [mWARNING: [mnot loading 'libfareygen.dylib' (incompatible with this version of Csound (3.1/3.0)[m
    [mWARNING: [mnot loading 'libfaustcsound.dylib' (incompatible with this version of Csound (3.1/3.0)[m
    [mWARNING: [mnot loading 'libframebuffer.dylib' (incompatible with this version of Csound (3.1/3.0)[m
    [mWARNING: [mnot loading 'libhdf5ops.dylib' (incompatible with this version of Csound (3.1/3.0)[m
    [mWARNING: [mnot loading 'libimage.dylib' (incompatible with this version of Csound (3.1/3.0)[m
    [mWARNING: [mnot loading 'libipmidi.dylib' (incompatible with this version of Csound (3.1/3.0)[m
    [mWARNING: [mnot loading 'libjackTransport.dylib' (incompatible with this version of Csound (3.1/3.0)[m
    [mWARNING: [mnot loading 'libpadsynth.dylib' (incompatible with this version of Csound (3.1/3.0)[m
    [mWARNING: [mnot loading 'libplaterev.dylib' (incompatible with this version of Csound (3.1/3.0)[m
    [mWARNING: [mnot loading 'libpy.dylib' (incompatible with this version of Csound (3.1/3.0)[m
    [mWARNING: [mnot loading 'librtjack.dylib' (incompatible with this version of Csound (3.1/3.0)[m
    [mWARNING: [mnot loading 'libscansyn.dylib' (incompatible with this version of Csound (3.1/3.0)[m
    [mWARNING: [mnot loading 'libserial.dylib' (incompatible with this version of Csound (3.1/3.0)[m
    [mCsound tidy up: Segmentation fault
     
  23. rorywalsh

    rorywalsh

    Joined:
    Apr 10, 2015
    Posts:
    114
    Those warnings are not fatal. The segmentation fault is though. And that may be coming from your Csound source code. Do you have a version of Csound already installed on your machine? If so, you can try the following, remove the following lines from CsoundUnityBridge:

    https://github.com/rorywalsh/CsoundUnity/blob/master/CsoundUnityScripts/CsoundUnityBridge.cs#L48-L49

    That should force CsoundUnity to use the system Csound. It might get rid of those warnings, but the seg fault is coming from somewhere else.
     
  24. g__b

    g__b

    Joined:
    Sep 17, 2014
    Posts:
    39
    After removing the two lines it is working, stereo too!

    Installed Csound version: 6.03.2 (double samples) Jul 3 2014

    Thanks again
     
  25. rorywalsh

    rorywalsh

    Joined:
    Apr 10, 2015
    Posts:
    114
    Great. I am giving a demo on this stuff in a few weeks time at a Csound gathering so I'll be sure to update the OSX package before then. There's nothing wrong with using the system Csound for developing, but when you export your game you will need to make sure you copy the CsoundLib framework from your libraries folder to the StreamingAssets folder of you project. Anyway, if you need to export as a standalone just let me know. I can talk you through it.
     
  26. g__b

    g__b

    Joined:
    Sep 17, 2014
    Posts:
    39
  27. rorywalsh

    rorywalsh

    Joined:
    Apr 10, 2015
    Posts:
    114
    Android would need a bit of work. That link might contain all you need, but I haven't used Csound on Android for a year or two. I do have my own pre-built libs that might do the trick. But it would take me some time to get it going correctly.
     
  28. g__b

    g__b

    Joined:
    Sep 17, 2014
    Posts:
    39
    Thank you for now, I'll let you know if I succeed with Android.
     
  29. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,462
    Really cool project. Thanks for awesome work.
    Wondering if you know this works on WebGL?
     
  30. rorywalsh

    rorywalsh

    Joined:
    Apr 10, 2015
    Posts:
    114
    It should be possible but it's not something I've ever looked into. I was able to build a simple demo using the old Unity web player and an emscripten build of Csound. It was quite easy to get Unity and Csound speaking to each other that way, but I've not looked at the newer WebGL stuff at all.
     
  31. creativewax

    creativewax

    Joined:
    Jan 28, 2015
    Posts:
    23
    Wondering. Does this lift the lid on the 8 channel limit Unity has on audio? I have a project that requires around 16 channels of audio and looking at options in moving forward. Suggestions welcome :)
     
  32. rorywalsh

    rorywalsh

    Joined:
    Apr 10, 2015
    Posts:
    114
    It will, IF you use Csound's own audio IO interfaces. My first iteration of this wrapper did this, but I've since changed it so that Csound is merely a processor which fires audio samples to Unity for playback. It wouldn't take much at all for you to revert it back. If you're interested and wants to discuss this more, head on over to the CsoundForGames forum at cabbageaudio.com I check those forums more than these nowadays.
     
    twobob likes this.
  33. creativewax

    creativewax

    Joined:
    Jan 28, 2015
    Posts:
    23
    thanks man. I have started a new topic on it now. Any help appreciated :)
     
  34. g__b

    g__b

    Joined:
    Sep 17, 2014
    Posts:
    39
    Happy to announce that we have officially released CsoundUnity version 3.0!
    Here is the ChangeLog:

    New Features
    • Restructured the code in the form of a Unity Package (https://docs.unity3d.com/Manual/PackagesList.html).
    • Updated libraries to Csound 6.15.
    • Implemented most of the Csound API.
    • Several improvements of the Editor inspector:
      • Now the changes made to the Control Channels found in the csd are correctly serialised and saved in the Scene, and are fully compatible with Unity Inspector presets.
      • Added Edit Csd Section, to be able to edit the csd from Unity, and save its content on disk.
      • Added Test Score Section, to be able to send score to Unity when testing in Editor.
      • Added AudioChannels: the csd file is scanned for chnseta opcodes, and the resulting Audio Channels can be seen in the inspector, and publicly accessed from a dictionary.
      • Added folded groups:
        • Settings
        • Edit Csd Section
        • Test Score Section
        • Control Channels
        • AudioChannels
    • Added CsoundUnityChild, to be able to read audio from the AudioChannels of a CsoundUnity instance.
    • Added CsoundFileWatcher to detect changes made by an external program to the csds used in the scene, and update them. Add FILEWATCHER_ON in your project Scripting Define Symbols.
    • Added Android libraries, builds working on Android 64bit (it has been successfully tested on Oculus Quest).
    • Added an utility method to load samples from AudioClips (currently from Resources folder only).
    • Added CreateTable methods, to be able to create Csound tables from float arrays.
    • Added a toggle to show warnings and hard filter output samples with values higher than a threshold.
    • Cabbage widgets: Added support for ComboBoxes.
    • Added IsInitialized property and OnCsoundInitialized event.
    • Added PerformanceFinished property.
    • Added Samples: Csound Test, Basic Test, CsoundUnityChild Test, Simple Sequencer, AudioClip Reader.
    • Overriding the sample rate and control rate of the csd using Unity Audio Project Settings.
    • Improved Logging.
    • Updated macOS library to use a .bundle instead of a .framework, to be able to build for macOS straight out of the box.
    • Totally removed the need to use the StreamingAssets folder to store csds, libraries and audio files, the csd is saved into the CsoundUnity instance as soon as it is dragged in the Csd Asset inspector field.
    • Removed the overwriting of the Path variable of the operating system.
    Fixes
    • Crash on exit.
    • Fixes for distorted audio output when reading mono files.
    • Build/Editor DLLNotFound issues on macOS.
    Where to ask for help, report bugs, ask for new features:
    Cabbage forum: use this forum for everything related to the use and features of CsoundUnity, ask for help, report bugs, ask for new features.
    Csound forum: help with Csound code, report bugs / ask for features relative to the Csound library.
    Csound dischord chat: where Csound developers meet.
    CsoundUnity Github repository: report specific issues with CsoundUnity, ask for new features, contribute to the development.

    And now party time!

    Very curious to see what you will create with it!
     
    Last edited: May 3, 2021
    nathanjams, aihodge and BenniKo like this.