HISE Logo Forum
    • Categories
    • Register
    • Login

    template or tutorial for custom c++ scriptnode?

    Scheduled Pinned Locked Moved ScriptNode
    38 Posts 8 Posters 1.2k 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.
    • MorphoiceM
      Morphoice
      last edited by

      Ahoi!
      is there a template or tutorial out there for making custom scriptnodes in C++?

      i'm aiming to make a simple 8 bit bitcrusher, however not linear but similar to μ-law
      i cant see another way to do it than to dive into c++ scriptnodes. The function is fairly simple and openly available but how to wrap it into a scriptnode I don't know.

      Also open for other solutions

      https://instagram.com/morphoice - 80s inspired Synthwave Music, Arcade & Gameboy homebrew!

      Christoph HartC 1 Reply Last reply Reply Quote 0
      • Christoph HartC
        Christoph Hart @Morphoice
        last edited by

        @Morphoice try snex, should be doable within its limitations.

        MorphoiceM 2 Replies Last reply Reply Quote 0
        • MorphoiceM
          Morphoice @Christoph Hart
          last edited by

          @Christoph-Hart never gotten the hang of snex but this might be worth looking into for that kind of thing. C++ might be overkill.

          https://instagram.com/morphoice - 80s inspired Synthwave Music, Arcade & Gameboy homebrew!

          HISEnbergH 1 Reply Last reply Reply Quote 0
          • HISEnbergH
            HISEnberg @Morphoice
            last edited by

            @Morphoice @griffinboy has put together a neat tutorial for c++ nodes for if/when you are interested
            https://forum.hise.audio/topic/10591/tutorial-how-to-create-a-c-custom-node?_=1733752760689

            MorphoiceM 2 Replies Last reply Reply Quote 0
            • MorphoiceM
              Morphoice @Christoph Hart
              last edited by

              @Christoph-Hart i can't even get round() to work in SNEX ;)

              https://instagram.com/morphoice - 80s inspired Synthwave Music, Arcade & Gameboy homebrew!

              1 Reply Last reply Reply Quote 0
              • MorphoiceM
                Morphoice @HISEnberg
                last edited by

                @HISEnberg ah yes, that's the one we've been talking about in the hangout, I couldn#t find it anymore for some reason. thanks

                https://instagram.com/morphoice - 80s inspired Synthwave Music, Arcade & Gameboy homebrew!

                1 Reply Last reply Reply Quote 0
                • MorphoiceM
                  Morphoice @HISEnberg
                  last edited by

                  @HISEnberg I did it, based on @griffinboy's template, yeeha!

                  This has a simple 8-bit bitcrusher and the right left gain from the demo like so:

                  void process(float* samples, int numSamples)
                              {
                                  for (int i = 0; i < numSamples; ++i)
                                  {
                                      // Reduce bit depth
                                      float max = 255;
                                      float val = samples[i];
                                      float rem = fmodf(val,1/max);
                                      
                                      // Quantize
                                      samples[i] = val - rem;
                                      
                                      // Apply Demo Gain
                                      samples[i] *= smoothGain.advance();
                                  }
                              }
                  

                  Screenshot 2024-12-09 at 16.06.06.png

                  now I have to figure out how to make it μ-law instead of linear 8 bit.
                  I'm afraid the code won't be that simple. If anyone has tried this before I'd appreciate any input.

                  https://instagram.com/morphoice - 80s inspired Synthwave Music, Arcade & Gameboy homebrew!

                  HISEnbergH 1 Reply Last reply Reply Quote 3
                  • HISEnbergH
                    HISEnberg @Morphoice
                    last edited by

                    @Morphoice Nice you move fast!

                    MorphoiceM 1 Reply Last reply Reply Quote 1
                    • MorphoiceM
                      Morphoice @HISEnberg
                      last edited by

                      @HISEnberg not fast enough. 90% is trial and error and me not understanding a thing I do lol

                      https://instagram.com/morphoice - 80s inspired Synthwave Music, Arcade & Gameboy homebrew!

                      d.healeyD 1 Reply Last reply Reply Quote 0
                      • d.healeyD
                        d.healey @Morphoice
                        last edited by

                        @Morphoice said in template or tutorial for custom c++ scriptnode?:

                        not fast enough

                        You're plenty fast!

                        Libre Wave - Freedom respecting instruments and effects
                        My Patreon - HISE tutorials
                        YouTube Channel - Public HISE tutorials

                        MorphoiceM 1 Reply Last reply Reply Quote 1
                        • MorphoiceM
                          Morphoice @d.healey
                          last edited by Morphoice

                          @HISEnberg @d-healey actually tried implementing the μ-law compression thing from what I could learn around the web, but it's failing to compile and I cant get a debug version setup on mac as @griffinboy's tutorial only covers PC so far ;) So now I'm stuck lol. But yeah, the problem should be minor and some day I'll find out ;)

                                              float val = samples[i];
                                                
                                              // mu-law compress signal
                                              float max = 255;
                                              float valmu  = sgn(val) * log(1+max*abs(val)) / log(1+max);
                                              
                                              // Quantize to 8 bits
                                              float valmu8 = valmu - fmodf(valmu,1/max);
                                              
                                              // mu-law expand signal
                                              float valex = sgn(valmu8) * (1/max) * (pow(max+1,abs(valmu8))-1);
                                              
                                              // Apply Demo Gain
                                              samples[i] = valex * smoothGain.advance();
                          

                          I basically know too litte about c++ to know if the syntax in these formulas is even valid but I tried ;)

                          https://instagram.com/morphoice - 80s inspired Synthwave Music, Arcade & Gameboy homebrew!

                          d.healeyD 1 Reply Last reply Reply Quote 0
                          • d.healeyD
                            d.healey @Morphoice
                            last edited by

                            @Morphoice said in template or tutorial for custom c++ scriptnode?:

                            but I tried

                            Some of us try for years, don't give up after 2 hours :)

                            Libre Wave - Freedom respecting instruments and effects
                            My Patreon - HISE tutorials
                            YouTube Channel - Public HISE tutorials

                            MorphoiceM 1 Reply Last reply Reply Quote 0
                            • MorphoiceM
                              Morphoice @d.healey
                              last edited by

                              @d-healey thanks to your encouraging words it just came to me that actually I just needed to implement the sgn (sign) function as C++ doesn't seem to have one by default

                                  template <typename T> int sgn(T val) {
                                      return (T(0) < val) - (val < T(0));
                                  }
                              

                              (yeah that one I copied off the internet lol)

                              and then it suddenly worked. I can't believe it! I have just made a μ-law bitcrusher without even so much as knowing what I do lol. God this thing sounds fly. Massively less artifacts on quieter sounds and sizzle on loud ones!

                              https://instagram.com/morphoice - 80s inspired Synthwave Music, Arcade & Gameboy homebrew!

                              Christoph HartC 1 Reply Last reply Reply Quote 0
                              • Christoph HartC
                                Christoph Hart @Morphoice
                                last edited by

                                @Morphoice use hmath::sign() :)

                                MorphoiceM 1 Reply Last reply Reply Quote 1
                                • MorphoiceM
                                  Morphoice @Christoph Hart
                                  last edited by Morphoice

                                  @Christoph-Hart that would have been to easy ;))
                                  In all honesty, I seriously need to learn C++, the things one can do...

                                  https://instagram.com/morphoice - 80s inspired Synthwave Music, Arcade & Gameboy homebrew!

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

                                    @Morphoice everything should work in your code once you:

                                    • use the real math functions as Chris suggested
                                    • respect the variable types. SNEX is very picky about this which is a good thing, but the errors messages are sometimes not helpful (you get the cast issue at the right line for expressions, but not in variable definitions). In your case, when a var type is float, EVERYTHING needs to be float in the expression. (1.0f)

                                    Another suggestion in SNEX, is that you should try to compile for each single line you add to your code, this is especially true at beginning. Because debugging multiple lines you added will be a nightmare as the console writes everything on the same line (remember the Hise Hang ☺)
                                    This reminds me to have a look at least to fix this issue...
                                    So write a new line -> compile, and if it fails you know where you are 😉

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

                                    MorphoiceM 1 Reply Last reply Reply Quote 0
                                    • MorphoiceM
                                      Morphoice @ustk
                                      last edited by

                                      @ustk aye! I did this in C++ not SNEX though. But I agree it looks more correct this way, although it didn't throw a tantrum without. C++ seems to be a bit forgiving there, which is not at all what I heard about it lol

                                      float val = samples[i];
                                      int max = 255;
                                      float valmu  = sgn(val) * log( 1.0f + max * abs(val) ) / log( 1.0f + max);
                                      float valmu8 = valmu - fmodf(valmu, 1.0f / max);
                                      float valex = sgn(valmu8) * (1.0f / max) * ( pow(max + 1.0f, abs(valmu8)) - 1.0f );                   
                                      

                                      https://instagram.com/morphoice - 80s inspired Synthwave Music, Arcade & Gameboy homebrew!

                                      MorphoiceM 1 Reply Last reply Reply Quote 1
                                      • MorphoiceM
                                        Morphoice @Morphoice
                                        last edited by

                                        @Christoph-Hart is there a way to get the custom c++ scriptnode to only process data if there is any sound? as this is intended for one-shots but constantly does the encoding/decoding and a couple of those nodes produce quite a fair amount of cpu

                                        https://instagram.com/morphoice - 80s inspired Synthwave Music, Arcade & Gameboy homebrew!

                                        ustkU griffinboyG 2 Replies Last reply Reply Quote 0
                                        • ustkU
                                          ustk @Morphoice
                                          last edited by ustk

                                          @Morphoice I can think of two ways:

                                          • either build a detector that is waiting for a certain value to pass a threshold, then if it does, do your stuff
                                          • tell the node a signal is coming with a modulated parameter, which will move the detection part to the graph

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

                                          MorphoiceM 1 Reply Last reply Reply Quote 1
                                          • MorphoiceM
                                            Morphoice @ustk
                                            last edited by

                                            @ustk are all nodes generally on? I've read about a hasTail parameter that stops processing when the note is done etc. probably in my case it needs to work only a few ms after a note was fired

                                            https://instagram.com/morphoice - 80s inspired Synthwave Music, Arcade & Gameboy homebrew!

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

                                            16

                                            Online

                                            1.7k

                                            Users

                                            11.9k

                                            Topics

                                            103.3k

                                            Posts