HISE Logo Forum
    • Categories
    • Register
    • Login

    Snex Function - return an array

    Scheduled Pinned Locked Moved ScriptNode
    6 Posts 2 Posters 189 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • griffinboyG
      griffinboy
      last edited by griffinboy

      Do snex functions support returning a table?
      I've been struggling.

      My attempt has an error:

      fe14a41e-0e5e-4a93-b2f6-2bc9417a8c7c-image.png

      Entire Snex Node Code:

      template <int NV> struct Germanium1
      {
      	SNEX_NODE(Germanium1);
      	
      	
      
      	
      // Define the WDFComponent class
      struct WDFComponent {
          // Function to calculate impedance (returns the magnitude of a complex number)
          double calculateImpedance(double real, double imag) {
              // Calculate the magnitude of the complex number
              return Math.sqrt(real * real + imag * imag);
          }
      
          // Function to connect to another WDFComponent
          void connect(double otherComponent, double portName) {
              // Placeholder
          }
      };
      
      
      
      
      
      // Define the WDFCapacitor struct
      struct WDFCapacitor {
          WDFComponent base;  // Base component
          double capacitance = 0.0; // Capacitance value
      };
      // Function to calculate the impedance of the capacitor
      double calculateCapacitorImpedance(WDFCapacitor self, double sReal, double sImag) {
          float capacitance = self.capacitance;
      
          // Calculate the denominator: s * capacitance
          float denomReal = sReal * capacitance;
          float denomImag = sImag * capacitance;
      
          // Calculate the magnitude and angle of the denominator
          float denomMag = Math.sqrt(denomReal * denomReal + denomImag * denomImag);
          float denomAngle = Math.atan2(denomImag, denomReal);
      
          // Calculate the reciprocal of the magnitude
          float impedanceMag = 1.0 / denomMag;
          float impedanceAngle = -denomAngle;
      
          // Convert back to rectangular form
          float impedanceReal = impedanceMag * Math.cos(impedanceAngle);
          float impedanceImag = impedanceMag * Math.sin(impedanceAngle);
      
          // Return the impedance values as a tuple
          return [impedanceReal, impedanceImag];
      }
      
      // Function to connect the capacitor to another component
      float connectCapacitor(WDFCapacitor self, WDFComponent other, float portName) {
          // Implementation for connecting to another component
          Console.print("Connected to port: " + portName);
      }
      
      
      
      
      
      // Main function to test the WDFComponent class
      void main() {
      
      }
      
      
      
      	
      	
      	// Initialise the processing specs here
      	void prepare(PrepareSpecs ps)
      	{
      		// Run the main function
      		// main();
      	}
      	
      	// Reset the processing pipeline here
      	void reset()
      	{
      		
      	}
      	
      	// Process the signal here
      	template <typename ProcessDataType> void process(ProcessDataType& data)
      	{
      		
      	}
      	
      	// Process the signal as frame here
      	template <int C> void processFrame(span<float, C>& data)
      	{
      	}
      	
      	// Process the MIDI events here
      	void handleHiseEvent(HiseEvent& e)
      	{
      		
      	}
      	
      	// Use this function to setup the external data
      	void setExternalData(const ExternalData& d, int index)
      	{
      		
      	}
      	
      	// Set the parameters here
      	template <int P> void setParameter(double v)
      	{
      		
      	}
      };
      
      
      ustkU 2 Replies Last reply Reply Quote 0
      • ustkU
        ustk @griffinboy
        last edited by ustk

        @griffinboy In SNEX, arrays are span

        // init an array of type float and length 2
        span<float, 2> realAndImg = {0.0f};
        

        Then in the function

            realAndImg[0] = impedanceMag * Math.cos(impedanceAngle);
            realAndImg[1] = impedanceMag * Math.sin(impedanceAngle);
        
            return realAndImg;
        

        But it's not guaranteed error free

        Can't help pressing F5 in the forum...

        1 Reply Last reply Reply Quote 0
        • ustkU
          ustk @griffinboy
          last edited by

          @griffinboy Oh yeah and since your returning a span, the fonction can't cast double.
          You want to make your own type

          using MySpanType = span<float, 2>;
          MySpanType realAndImg = {0.0f};
          	
          // Function to calculate the impedance of the capacitor
          MySpanType calculateCapacitorImpedance()
          {
              // now you can return your span here
          }
          

          Can't help pressing F5 in the forum...

          griffinboyG 1 Reply Last reply Reply Quote 0
          • griffinboyG
            griffinboy @ustk
            last edited by griffinboy

            @ustk
            Woah.
            Thank you very much.

            I haven't used advanced stuff like this before, I usually program very simple things up until this point!

            I didn't even know you could do this stuff in hise! custom types? Structures? insane.

            Thanks for your help, I now see that my reply in your latest forum post is completely useless, you know much more about this than me! Apologies.

            Anyways, once I finish this script, I'll make a post about it and give away the code.
            It's a framework for modular circuit emulation using Wave Digital Filters, which will allow you to string together any virtual circuit as long as you know the schematics. I'm planning on including premade components from famous units, starting with everything found in an amp.

            ustkU 1 Reply Last reply Reply Quote 0
            • ustkU
              ustk @griffinboy
              last edited by ustk

              @griffinboy Oh no worries mate !
              Yeah SNEX is C++ based -> https://docs.hise.dev/scriptnode/manual/snex.html
              So you can do pretty (all?) thing from C++, plus a layer of smart methods and shortcuts

              If you're into WDF, why not going with Faust in the first place (within Scriptnode)? It's meant for WDF after all

              Can't help pressing F5 in the forum...

              griffinboyG 1 Reply Last reply Reply Quote 0
              • griffinboyG
                griffinboy @ustk
                last edited by griffinboy

                @ustk

                Thanks for the recommendation I'm not really familiar with faust.
                I'll look into it.

                Does faust support complex imaginary numbers?
                That's somthing that I would need for sure.

                Edit*
                Yeah faust seems a good route for the future.
                It looks really neat for this kind of thing. But for now I will finish my model in snex, as there is a tight deadline on this project.

                If only I can figure out the snex syntax! The little differences between snex and C++ really trip me up.
                It's maddening lol.

                3cd3dd0b-b351-48a9-b866-73cf02f6182b-image.png

                Pointers not working lol

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post

                19

                Online

                1.7k

                Users

                11.9k

                Topics

                103.4k

                Posts