Class WaveSynthBuilder

java.lang.Object
net.paulhertz.pixelaudio.WaveSynthBuilder

public class WaveSynthBuilder extends Object
Utilities for generating WaveSynth "instruments" and working with WaveSynth data. TODO piano key methods and semitoneFac are in AudioUtility, refactor to use them
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static int
    The default number of frames in a WaveSynth animation.
    static final double
    The factor by which to multiply a frequency to obtain a semitone increment.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    Applies the current phase values to the initial values of the WaveSynth, so that the current state of the image display will appear as the first frame of animation.
    static boolean
    checkJSONHeader(processing.data.JSONObject json, String key, String val)
    Checks for a header in the supplied JSON object that indicates that it contains WaveSynth data.
    static float[]
    chromaticScale(float funda)
    Generates an array of frequencies that correspond to the chromatic scale, starting with the supplied fundamental frequency.
    static int
    colorShift(int c, float shift)
    Moves an RGB color into the HSB color space and shifts it by a decimal fraction in the range (0.0,1.0) that represents a portion of the circle of hue values.
    static float
    frequencyPianoKey(float freq)
    Returns the piano key number that corresponds to a given frequency, where A4 (the A above middle C) is key number 49 and has a frequency of 440 Hz.
    static boolean
    getJSONFromFile(File selection, WaveSynth synth)
     
    static boolean
    getJSONFromFile(File selection, WaveSynth synth, boolean isStrict)
     
    static processing.data.JSONObject
    Creates a JSON object containing header information for a WaveSynth JSON file.
    static void
    muteWaveData(WaveSynth wavesynth, int elem)
    Mutes a WaveData operator (view in the control panel).
    static float
    pianoKeyFrequency(float keyNumber)
    Returns the frequency of a piano key number, where A4 (the A above middle C) is key number 49 and has a frequency of 440 Hz.
    static void
    Prints mute/active status of WaveData operators in supplied waveDataList.
    static String
    saveWaveSynthJSON(processing.core.PApplet app, File selection, WaveSynth synth)
    Saves a WaveSynth's settings and WaveData list to a JSON file.
    static void
    scaleAmps(ArrayList<WaveData> waveDataList, float scale, boolean isVerbose)
    Scales the amplitude of an ArrayList of WaveData objects.
    static void
    scaleFreqs(ArrayList<WaveData> waveDataList, float scale, boolean isVerbose)
    Scales the frequencies of an ArrayList of WaveData objects.
    static void
    setWaveSynthFromJSON(processing.data.JSONObject json, WaveSynth synth)
    Sets the fields of a WaveSynth using values stored in a JSON object.
    static void
    shiftColors(ArrayList<WaveData> waveDataList, float shift, boolean isVerbose)
    Shifts the colors of an ArrayList of WaveData objects.
    static void
    shiftPhases(ArrayList<WaveData> waveDataList, float shift, boolean isVerbose)
    Shifts the phase of an ArrayList of WaveData objects.
    static void
    Outputs the phase values of a WaveSynth's array of WaveData objects to the console.
    synthDoubleStop(float funda1, float funda2, float pianoKey1, float pianoKey2, int animSteps)
    Generates an ArrayList of WaveData objects to be used by a WaveSynth to generate RGB pixel values and (on request) audio signal values.
    synthTrumpet(float fundamental, int howManyPartials, float pianoKey, int animSteps)
    Generates an ArrayList of WaveData objects to be used by a WaveSynth to generate RGB pixel values and (on request) audio signal values.
    static void
    toggleWDMute(WaveSynth wavesynth, int elem)
    Mutes or unmutes a WaveData operator (view in the control panel).
    static void
    toggleWDSolo(WaveSynth wavesynth, int elem, boolean isSolo)
    Sets a specified WaveData element in the waveDataList of a supplied WaveSynth to WaveData.WaveState.SOLO if isSolo is true, otherwise sets it to WaveData.WaveState.ACTIVE.
    static void
    Unmutes all the operators in supplied waveDataList.
    static void
    unmuteWaveData(WaveSynth wavesynth, int elem)
    Unmutes a WaveData operator (view in the control panel).
    static String
    Returns supplied WaveSynth settings and WaveData list as a formated String.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • semitoneFac

      public static final double semitoneFac
      The factor by which to multiply a frequency to obtain a semitone increment.
    • animSteps

      public static int animSteps
      The default number of frames in a WaveSynth animation.
  • Constructor Details

    • WaveSynthBuilder

      public WaveSynthBuilder()
  • Method Details

    • synthTrumpet

      public static ArrayList<WaveData> synthTrumpet(float fundamental, int howManyPartials, float pianoKey, int animSteps)
      Generates an ArrayList of WaveData objects to be used by a WaveSynth to generate RGB pixel values and (on request) audio signal values.
      Parameters:
      fundamental - the fundamental frequency of the tone to be generated
      howManyPartials - the number of partials to include in the tone, 1 is just the fundamental, 2 is the fundamental and the second harmonic, etc.
      pianoKey - the piano key number (1-88) that corresponds to the fundamental frequency, used to shift the colors of the WaveData objects in the returned list
      animSteps - the number of frames in the animation.
      Returns:
      an ArrayList of WaveData objects
    • synthDoubleStop

      public static ArrayList<WaveData> synthDoubleStop(float funda1, float funda2, float pianoKey1, float pianoKey2, int animSteps)
      Generates an ArrayList of WaveData objects to be used by a WaveSynth to generate RGB pixel values and (on request) audio signal values.
      Parameters:
      funda1 - the fundamental frequency of the first tone to be generated
      funda2 - the fundamental frequency of the second tone to be generated
      pianoKey1 - the piano key number (1-88) that corresponds to the fundamental frequency of the first tone, used to shift the colors of the WaveData objects in the returned list that correspond to the first tone
      pianoKey2 - the piano key number (1-88) that corresponds to the fundamental frequency of the second tone, used to shift the colors of the WaveData objects in the returned list that correspond to the second tone
      animSteps - the number of frames in the animation
      Returns:
      an ArrayList of WaveData objects
    • chromaticScale

      public static float[] chromaticScale(float funda)
      Generates an array of frequencies that correspond to the chromatic scale, starting with the supplied fundamental frequency.
      Parameters:
      funda -
      Returns:
    • pianoKeyFrequency

      public static float pianoKeyFrequency(float keyNumber)
      Returns the frequency of a piano key number, where A4 (the A above middle C) is key number 49 and has a frequency of 440 Hz.
      Parameters:
      keyNumber -
      Returns:
    • frequencyPianoKey

      public static float frequencyPianoKey(float freq)
      Returns the piano key number that corresponds to a given frequency, where A4 (the A above middle C) is key number 49 and has a frequency of 440 Hz.
      Parameters:
      freq -
      Returns:
    • colorShift

      public static int colorShift(int c, float shift)
      Moves an RGB color into the HSB color space and shifts it by a decimal fraction in the range (0.0,1.0) that represents a portion of the circle of hue values. For example, 1.0f/24 is 15 degrees around the 360-degree HSB color circle.
      Parameters:
      c - an RGB color
      shift - the shift [0..1] of the hue in the HSB representation of color c
      Returns:
      the RGB representation of the shifted color
    • scaleAmps

      public static void scaleAmps(ArrayList<WaveData> waveDataList, float scale, boolean isVerbose)
      Scales the amplitude of an ArrayList of WaveData objects.
      Parameters:
      waveDataList - an ArrayList of WaveData objects
      scale - the amount to scale the amplitude of each WaveData object
      isVerbose - if true, output changes to data to the console
    • shiftColors

      public static void shiftColors(ArrayList<WaveData> waveDataList, float shift, boolean isVerbose)
      Shifts the colors of an ArrayList of WaveData objects.
      Parameters:
      waveDataList - an ArrayList of WaveData objects
      shift - the amount shift each color
      isVerbose - if true, output changes to data to the console
    • scaleFreqs

      public static void scaleFreqs(ArrayList<WaveData> waveDataList, float scale, boolean isVerbose)
      Scales the frequencies of an ArrayList of WaveData objects.
      Parameters:
      waveDataList - an ArrayList of WaveData objects
      scale - the amount to scale the frequency of each WaveData object
      isVerbose - if true, output changes to data to the console
    • shiftPhases

      public static void shiftPhases(ArrayList<WaveData> waveDataList, float shift, boolean isVerbose)
      Shifts the phase of an ArrayList of WaveData objects.
      Parameters:
      waveDataList - an ArrayList of WaveData objects
      shift - amount to shift the phase of each WaveData object
      isVerbose - if true, output changes to data to the console
    • showPhaseValues

      public static void showPhaseValues(WaveSynth wavesynth)
      Outputs the phase values of a WaveSynth's array of WaveData objects to the console.
      Parameters:
      wavesynth - a WaveSynth whose waveDataList's phase values will be output
    • capturePhaseValues

      public static void capturePhaseValues(WaveSynth wavesynth)
      Applies the current phase values to the initial values of the WaveSynth, so that the current state of the image display will appear as the first frame of animation. Save the WaveSynth to a JSON file to keep the new phase values.
      Parameters:
      wavesynth - an WaveSynth whose current WaveData phase values will be captured
    • toggleWDMute

      public static void toggleWDMute(WaveSynth wavesynth, int elem)
      Mutes or unmutes a WaveData operator (view in the control panel).
      Parameters:
      wavesynth - a WaveSynth
      elem - the index number of a WaveData object stored in a WaveSynth's waveDataList field
    • muteWaveData

      public static void muteWaveData(WaveSynth wavesynth, int elem)
      Mutes a WaveData operator (view in the control panel).
      Parameters:
      wavesynth - a WaveSynth
      elem - the index number of a WaveData object stored in a WaveSynth's waveDataList field
    • unmuteWaveData

      public static void unmuteWaveData(WaveSynth wavesynth, int elem)
      Unmutes a WaveData operator (view in the control panel).
      Parameters:
      wavesynth - a WaveSynth
      elem - the index number of a WaveData object stored in a WaveSynth's waveDataList field
    • unmuteAllWD

      public static void unmuteAllWD(ArrayList<WaveData> waveDataList)
      Unmutes all the operators in supplied waveDataList.
      Parameters:
      waveDataList - an ArrayList of WaveData objects
    • toggleWDSolo

      public static void toggleWDSolo(WaveSynth wavesynth, int elem, boolean isSolo)
      Sets a specified WaveData element in the waveDataList of a supplied WaveSynth to WaveData.WaveState.SOLO if isSolo is true, otherwise sets it to WaveData.WaveState.ACTIVE. If isSolo is true, all other WaveData elements taht are ACTIVE will be set to SUSPENDED. Any elements that are currently muted will remain muted, any elements whose waveState is SOLO will remain unchanged. Call unmuteAllWD() to set all elements to ACTIVE. TODO improve on the logic for WaveState changes.
      Parameters:
      wavesynth - a WaveSynth whose waveDataList will be affected
      elem - index of an element in the waveDataList
      isSolo - if true, set WaveData element to WaveData.WaveState.SOLO otherwise, set it to WaveData.WaveState.ACTIVE
    • printWDStates

      public static void printWDStates(ArrayList<WaveData> waveDataList)
      Prints mute/active status of WaveData operators in supplied waveDataList.
      Parameters:
      waveDataList - an ArrayList of WaveData objects
    • getJSONFromFile

      public static boolean getJSONFromFile(File selection, WaveSynth synth, boolean isStrict)
      Parameters:
      selection - a file containing JSON data
      json - a JSONObject that will be initialized with data from the file
      synth - a WaveSynth object whose fields will be filled out from the JSON data
      isStrict - if true, return false if no WaveSynth header is found in the JSON file
      Returns:
      true if the File
    • getJSONFromFile

      public static boolean getJSONFromFile(File selection, WaveSynth synth)
    • checkJSONHeader

      public static boolean checkJSONHeader(processing.data.JSONObject json, String key, String val)
      Checks for a header in the supplied JSON object that indicates that it contains WaveSynth data.
      Parameters:
      json - a JSONObject, typically read in from a file
      key - the key to look for in the JSON object, typically "PXAU"
      val - the value to compare against, typically "WSYN"
      Returns:
      true if the JSON object contains the specified key and value in its header, otherwise false
    • setWaveSynthFromJSON

      public static void setWaveSynthFromJSON(processing.data.JSONObject json, WaveSynth synth)
      Sets the fields of a WaveSynth using values stored in a JSON object.
      Parameters:
      json - a JSON object, typically read in from a file
      synth - a WaveSynth
    • waveSynthAsString

      public static String waveSynthAsString(WaveSynth synth)
      Returns supplied WaveSynth settings and WaveData list as a formated String.
      Parameters:
      synth - a WaveSynth
    • saveWaveSynthJSON

      public static String saveWaveSynthJSON(processing.core.PApplet app, File selection, WaveSynth synth)
      Saves a WaveSynth's settings and WaveData list to a JSON file.
      Parameters:
      app - a PApplet, used to call saveJSONObject()
      selection - a File object that represents the file to which the JSON data will be saved
      synth - the WaveSynth whose settings and WaveData list will be saved to the JSON file
    • getWaveSynthJSONHeader

      public static processing.data.JSONObject getWaveSynthJSONHeader()
      Creates a JSON object containing header information for a WaveSynth JSON file.
      Returns:
      a JSONObject containing the header information