Пример #1
0
    //--------------------------------------------------------------------------
    /// Initialise LibPd.
    void Awake()
    {
        // Initialise libpd, if it's not already.
        if (!pdInitialised)
        {
            // Setup hooks.
            printHook = new LibPdPrintHook(PrintOutput);
            libpd_set_queued_printhook(printHook);

            bangHook = new LibPdBangHook(BangOutput);
            libpd_set_queued_banghook(bangHook);

            floatHook = new LibPdFloatHook(FloatOutput);
            libpd_set_queued_floathook(floatHook);

            symbolHook = new LibPdSymbolHook(SymbolOutput);
            libpd_set_queued_symbolhook(symbolHook);

            listHook = new LibPdListHook(ListOutput);
            libpd_set_queued_listhook(listHook);

            messageHook = new LibPdMessageHook(MessageOutput);
            libpd_set_queued_messagehook(messageHook);

            noteOnHook = new LibPdMidiNoteOnHook(MidiNoteOnOutput);
            libpd_set_queued_noteonhook(noteOnHook);

            controlChangeHook = new LibPdMidiControlChangeHook(MidiControlChangeOutput);
            libpd_set_queued_controlchangehook(controlChangeHook);

            programChangeHook = new LibPdMidiProgramChangeHook(MidiProgramChangeOutput);
            libpd_set_queued_programchangehook(programChangeHook);

            pitchBendHook = new LibPdMidiPitchBendHook(MidiPitchBendOutput);
            libpd_set_queued_pitchbendhook(pitchBendHook);

            aftertouchHook = new LibPdMidiAftertouchHook(MidiAftertouchOutput);
            libpd_set_queued_aftertouchhook(aftertouchHook);

            polyAftertouchHook = new LibPdMidiPolyAftertouchHook(MidiPolyAftertouchOutput);
            libpd_set_queued_polyaftertouchhook(polyAftertouchHook);

            midiByteHook = new LibPdMidiByteHook(MidiByteOutput);
            libpd_set_queued_midibytehook(midiByteHook);

            // Initialise libpd if possible, report any errors.
            int initErr = libpd_queued_init();
            if (initErr != 0)
            {
                Debug.LogWarning("Warning; libpd_init() returned " + initErr);
                Debug.LogWarning("(if you're running this in the editor that probably just means this isn't the first time you've run your game, and is not a problem)");
            }
            pdInitialised = true;

            // Try and add the patch directory to libpd's search path for
            // loading externals (still can't seem to load externals when
            // running in Unity though).
            if (patchDir != String.Empty)
            {
                libpd_add_to_search_path(Application.streamingAssetsPath + patchDir);
            }

            // Make sure our static pipePrintToConsole variable is set
            // correctly.
            pipePrintToConsoleStatic = pipePrintToConsole;
        }
        else
        {
            pipePrintToConsole = pipePrintToConsoleStatic;
        }

        // Calc numTicks.
        int bufferSize;
        int noOfBuffers;

        AudioSettings.GetDSPBufferSize(out bufferSize, out noOfBuffers);
        numTicks = bufferSize / libpd_blocksize();

        // Create our instance.
        instance = libpd_new_instance();

        // Set our instance.
        libpd_set_instance(instance);

        // Initialise audio.
        int err = libpd_init_audio(2, 2, AudioSettings.outputSampleRate);

        if (err != 0)
        {
            pdFail = true;
            Debug.LogError(gameObject.name + ": Could not initialise Pure Data audio. Error = " + err);
        }
        else
        {
            if (patchName == String.Empty)
            {
                Debug.LogError(gameObject.name + ": No patch was assigned to this LibPdInstance.");
                patchFail = true;
            }
            else
            {
                //Create our bindings dictionary.
                bindings = new Dictionary <string, IntPtr>();

                // Open our patch.
                patchPointer = libpd_openfile(patchName + ".pd",
                                              Application.streamingAssetsPath + patchDir);
                if (patchPointer == IntPtr.Zero)
                {
                    Debug.LogError(gameObject.name +
                                   ": Could not open patch. Directory: " +
                                   Application.streamingAssetsPath + patchDir +
                                   " Patch: " + patchName + ".pd");
                    patchFail = true;
                }

                // Turn on audio processing.
                libpd_start_message(1);
                libpd_add_float(1.0f);
                libpd_finish_message("pd", "dsp");
            }
        }
    }
Пример #2
0
 private static extern void libpd_set_queued_messagehook(LibPdMessageHook hook);