Rotatosound

Developed or developing a new product in PureBasic? Tell the world about it.
doctornash
Enthusiast
Enthusiast
Posts: 130
Joined: Thu Oct 20, 2011 7:22 am

Rotatosound

Post by doctornash »

I publish here the full code for my sound synthesis app 'Rotatosound' (and following that the Portaudio code which is an 'Include' in the app). It was done with PB version 5.31.
It generates a changing sound waveform from the motion of the vertices of rotating 3D objects.
It runs fine on my Win XP, 7 and 10 machines with 'Use Debugger' unticked.
I've also uploaded an 'explanation and demo' video of it here https://youtu.be/OtjPJXZMNf4

Many thanks to the fine folk over at the '3D Programming' section of the forum who helped me go from zero knowledge of 3D programming a few short weeks ago to a point where I could cobble together a 'proof of concept' of the idea:
viewtopic.php?f=36&t=61455

Rotatasound code:

Code: Select all

XIncludeFile "PortAudio.pb"

#SampleRate = 44100
#PI2 = 6.28318530717958
Global Phase.d=0
Global PhaseAdd.d
Global Freq.d = 230
Global PosWvTble.d

Global FreqMultiplier.d = 2.2 ;can vary this between 1 and 2.5 for more of a 'wow' or more of a 'whoa' sound

Global NumNodes
Global NewNumNodes

Global Dim n.i(0)
Global mainnode.i

Global XInc.f = 0.8
Global YInc.f = 0
Global ZInc.f = 0.2

Global CameraZoom.d
Global VariableShapeZoom.d = 0

Global VariableShapeParam1.f = 1
Global VariableShapeParam2.f = 1

Declare AttachNodes()

Declare Tetrahedron()
Declare Airplane()
Declare HexPrism()

;For waveform co-ordinates:
Structure VertexPos
    XVal.f
    YVal.f
EndStructure

Global Dim VertexArr.VertexPos(0) ;real time for Y coords
Global Dim sampleArr.VertexPos(0) ;sampled arr for Y coords

Global SoundFlag = 0

Global FreqFollowFlag = 0
Global FreqPulseFlag = 0

Declare DrawWaveform()


Enumeration
 #Trackbar_X
 #Trackbar_Y
 #Trackbar_Z
 #GADGET_Canvas1
 #Button_Sound
 #Trackbar_Camera1
 #Option_Tetra
 #Option_VariableShape
 #Option_Plane
 
 #Trackbar_VariableShapeParam1
 #Trackbar_VariableShapeParam2
 #Trackbar_Frequency
 
 #Checkbox_Pulse
 #Checkbox_Frequency
 
 #Text_X
 #Text_Y
 #Text_Z
 #Text_Zoom
 
 #Frame_Rotation
 #Frame_Shape
 #Frame_Sound
EndEnumeration  

Declare.f CalculateTheWaveform()

Procedure Error(err)
  Debug Pa_GetErrorText(err)
  MessageRequester("", PeekS(Pa_GetErrorText(err)))
  End
EndProcedure

Define err.i
err = Pa_Initialize()
If err <> #paNoError
  Error(err)
EndIf


Procedure.f CalculateTheWaveform()
  
Protected ReturnVal.f

PhaseAdd = (Freq/#SampleRate)
Phase = Phase + PhaseAdd

If Phase>=1
  Phase = Phase-1
  CopyArray(VertexArr(),sampleArr())
EndIf  
          
PosWvTble = 320*Phase

ext.d=PosWvTble

listPos = (Int(PosWvTble/(320/(NewNumNodes+2))))+1

wy1.d = sampleArr(listPos)\YVal
ex1.d = sampleArr(listPos)\XVal

wy0.d = sampleArr(listPos-1)\YVal
ex0.d = sampleArr(listPos-1)\XVal

;LINEAR INTERPOLATION:
;wyt.f = ((wy1-wy0)*(ext-ex0)/(ex1-ex0))+wy0 

;CUBIC INTERPOLATION:
mu.f = ((-Cos(((ext-ex0)/(ex1-ex0))*#PI2/2))/2)+0.5
wyt.f = ((wy1-wy0)*mu)+wy0 

ReturnVal = (75-wyt)/75  
 
ProcedureReturn ReturnVal
     
EndProcedure  


ProcedureC PaStreamCallback(*in, *output.Float, frameCount, *timeInfo.PaStreamCallbackTimeInfo, statusFlags, *userData)
  While frameCount
     *output\f = CalculateTheWaveform() 
   *output + 4
   frameCount - 1
Wend
EndProcedure


Define op.PaStreamParameters
op\channelCount = 1
op\device = Pa_GetDefaultOutputDevice()
op\sampleFormat = #paFloat32
op\suggestedLatency = 75/1000
err = Pa_OpenStream(@Stream, #Null, @op, #SampleRate, #paframesperbufferunspecified, #paNoFlag, @PaStreamCallback(), 0)

If err <> #paNoError
  Error(err)
EndIf


If InitEngine3D()
  
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
  InitSprite()
  InitKeyboard()
  
  OpenWindow(0, 0, 0, 550, 425, "RotatoSound")
  OpenWindowedScreen(WindowID(0), 10, 10, 320,240, 0, 0, 0)
  
  CanvasGadget(#GADGET_Canvas1, 10, 260, 320, 150, #PB_Canvas_ClipMouse) ;for graphing the waveform
  
  StartDrawing(CanvasOutput(#GADGET_Canvas1))
  Box(0, 0, 320, 150, $000000)
  LineXY(0, 75, 320, 75, $0000FF)
  StopDrawing()
  
  OptionGadget(#Option_Tetra, 360, 160, 100, 20, "Tetrahedron")
  OptionGadget(#Option_VariableShape, 360, 200, 70, 20, "Variable")
  OptionGadget(#Option_Plane, 360, 240, 100, 20, "Plane-Fish")
  
  SetGadgetState(#Option_Plane,1)
  
  TrackBarGadget(#Trackbar_VariableShapeParam1, 430, 195, 100, 20, 0, 100)
  TrackBarGadget(#Trackbar_VariableShapeParam2, 430, 215, 100, 20, 0, 100)
  SetGadgetState(#Trackbar_VariableShapeParam1, 20)
  SetGadgetState(#Trackbar_VariableShapeParam2, 20)
  
  
  TrackBarGadget(#Trackbar_X, 390, 40, 140, 20, 0, 500)
  TrackBarGadget(#Trackbar_Y, 390, 60, 140, 20, 0, 500)
  TrackBarGadget(#Trackbar_Z, 390, 80, 140, 20, 0, 500)
  
  SetGadgetState(#Trackbar_X, 80)
  SetGadgetState(#Trackbar_Y, 0)
  SetGadgetState(#Trackbar_Z, 20)
  
  TextGadget(#text_X, 360,40,20,20, "X:")
  TextGadget(#text_Y, 360,60,20,20, "Y:")
  TextGadget(#text_Z, 360,80,20,20, "Z:")
  TextGadget(#text_Zoom, 360,110,30,20, "Zoom")
  
  FrameGadget(#Frame_Rotation, 340, 10, 200, 125, "Rotation")
  FrameGadget(#Frame_Shape, 340, 140, 200, 125, "Shape")
  FrameGadget(#Frame_Sound, 340, 270, 200, 140, "Sound")

  
  TrackBarGadget(#Trackbar_Camera1, 390, 110, 140, 20, 100, 1000)
  
  TrackBarGadget(#Trackbar_Frequency, 430, 340, 100, 20, 50, 400)
  SetGadgetState(#Trackbar_Frequency, 220)
  
  CheckBoxGadget(#Checkbox_Frequency, 360,  335, 70, 20, "Frequency")
  CheckBoxGadget(#Checkbox_Pulse, 360, 355, 80, 20, "Pulse")
  DisableGadget(#Checkbox_Pulse, 1)
  
  ButtonGadget(#Button_Sound, 360, 300, 60, 25, "sound ON",#PB_Button_Toggle)
  
  CreateCamera(0, 0, 0, 100, 100)
  
  Airplane()
  
      Define Quit.i
      Quit = #False
      
      Global FreqDelta.d = 0.05
      
      UpdateFlag = 0
      
  Repeat

    Repeat
      Event = WindowEvent()
      If Event = #PB_Event_CloseWindow
        Quit = #True
      EndIf  
      
      If Event = #PB_Event_Gadget
        Select EventGadget()
            Case #Trackbar_X
              XInc = GetGadgetState(#Trackbar_X) / 100
            Case #Trackbar_Y
              YInc = GetGadgetState(#Trackbar_Y) / 100 
            Case #Trackbar_Z
              ZInc = GetGadgetState(#Trackbar_Z) / 100  
            Case #Trackbar_Camera1
              CameraZoom = GetGadgetState(#Trackbar_Camera1)/100
              MoveCamera(0, 0, 0, CameraZoom, #PB_Absolute)
              Debug CameraZoom
                  If GetGadgetState(#Option_VariableShape)
                    VariableShapeZoom = CameraZoom
                  EndIf  
            Case #Trackbar_VariableShapeParam1
              If GetGadgetState(#Option_VariableShape)
                VariableShapeParam1 = (1/25)*GetGadgetState(#Trackbar_VariableShapeParam1)+0.2
                SetGadgetText(#Button_Sound, "sound ON")
                SetGadgetState(#Button_Sound,0)
                SoundFlag = 0
                pa_abortstream(stream)
                HexPrism()
              EndIf
            Case #Trackbar_VariableShapeParam2
              If GetGadgetState(#Option_VariableShape)
                VariableShapeParam2 = (1/25)*GetGadgetState(#Trackbar_VariableShapeParam2)+0.2 
                SetGadgetText(#Button_Sound, "sound ON")
                SetGadgetState(#Button_Sound,0)
                SoundFlag = 0
                pa_abortstream(stream)
                HexPrism()
              EndIf
            Case #Trackbar_Frequency
              FreqMultiplier = GetGadgetState(#Trackbar_Frequency)/100
            Case #Checkbox_Frequency
              If GetGadgetState(#Checkbox_Frequency) 
                FreqFollowFlag = 1
                    If GetGadgetState(#Option_Plane)
                      DisableGadget(#Checkbox_Pulse,0)
                    EndIf  
              Else
                FreqFollowFlag = 0
                SetGadgetState(#Checkbox_Pulse,0)
                DisableGadget(#Checkbox_Pulse,1)
                FreqPulseFlag = 0
              EndIf  
            Case #Checkbox_Pulse
              If GetGadgetState(#Checkbox_Pulse) 
                FreqPulseFlag = 1
              Else
                FreqPulseFlag = 0
              EndIf  
            Case #Button_Sound  
              If GetGadgetState(#Button_Sound)
                SetGadgetText(#Button_Sound, "sound OFF")
                SoundFlag = 1
                Pa_StartStream(stream)
              Else
                SetGadgetText(#Button_Sound, "sound ON")
                SoundFlag = 0
                Pa_AbortStream(stream)
              EndIf  
            Case #Option_Tetra
              SetGadgetText(#Button_Sound, "sound ON")
                SetGadgetState(#Button_Sound,0)
                SoundFlag = 0
                pa_abortstream(stream)  
                SetGadgetState(#Checkbox_Pulse,0)
                DisableGadget(#Checkbox_Pulse,1)
                FreqPulseFlag = 0
              Tetrahedron()
            Case #Option_VariableShape
              SetGadgetText(#Button_Sound, "sound ON")
                SetGadgetState(#Button_Sound,0)
                SoundFlag = 0
                pa_abortstream(stream)
                If VariableShapeZoom = 0
                  CameraZoom = 2.43
                  MoveCamera(0, 0, 0, CameraZoom, #PB_Absolute)
                  SetGadgetState(#Trackbar_Camera1, 243)
                Else
                  MoveCamera(0, 0, 0, VariableShapeZoom, #PB_Absolute)
                  SetGadgetState(#Trackbar_Camera1, VariableShapeZoom*100)
                EndIf  
                SetGadgetState(#Checkbox_Pulse,0)
                DisableGadget(#Checkbox_Pulse,1)
                FreqPulseFlag = 0
              HexPrism()
            Case #Option_Plane  
              SetGadgetText(#Button_Sound, "sound OFF")
                SetGadgetState(#Button_Sound,0)
                SoundFlag = 0
                pa_abortstream(stream)
                    If GetGadgetState(#Checkbox_Frequency)
                      DisableGadget(#Checkbox_Pulse,0)
                    EndIf  
              Airplane()
        EndSelect      
      EndIf
    Until Event = 0
    
        
    
    For x = 1 To NewNumNodes+1
        If Mod(x,2)
          VertexArr(x)\YVal = (((CameraProjectionX(0, NodeX(n((x-1)/2)), NodeY(n((x-1)/2)), NodeZ(n((x-1)/2))))*(150/240))-(150/6))
        Else
          VertexArr(x)\YVal = (CameraProjectionY(0, NodeX(n((x/2)-1)), NodeY(n((x/2)-1)), NodeZ(n((x/2)-1))))*(150/240)
        EndIf  

        If x = Int(0.4*NewNumNodes) ;this gives frequency change tied to x=5 node for the plane, which sounds good
          UpdateFlag = UpdateFlag + 1
          If UpdateFlag>1
                    FreqDelta = FreqDelta+0.05 ;Can try FreqDelta = FreqDelta+(Random(100,0)/1000), a different effect than incrementing frequency by fixed amount each iteration
                            If FreqDelta>0.5 ;Can try varying both the 0.5 value, and the increment (0.05) value for interesting effects
                              FreqDelta = 0.05
                            EndIf   
          UpdateFlag = 0
        EndIf
        
          If FreqFollowFlag
                  If FreqPulseFlag
                    Freq = (FreqDelta + FreqMultiplier)*VertexArr(x)\YVal 
                  Else
                    Freq = (FreqMultiplier)*VertexArr(x)\YVal
                  EndIf  
              Else
                Freq = FreqMultiplier*105
              EndIf  
        EndIf  
      Next 
      
      ;elm+1
      

      WidthOfXInc.f = 320/(NewNumNodes+2)
      For x = 1 To NewNumNodes+1
        VertexArr(x)\XVal = WidthOfXInc + (x-1)*WidthOfXInc
      Next
      
      VertexArr(0)\XVal = 0
      VertexArr(0)\YVal = 75
      VertexArr(NewNumNodes+2)\XVal = 320
      VertexArr(NewNumNodes+2)\YVal = 75
      
      DrawWaveform()    
        
      RotateNode(mainnode, XInc, YInc, ZInc, #PB_Relative)
  
    
    RenderWorld()
    FlipBuffers()
    
  Until Quit
    Pa_StopStream(stream)
    Pa_Terminate()
EndIf

End

Procedure AttachNodes()
Protected x.i
For x = 0 To NumNodes
  DetachNodeObject(n(x), NodeID(mainnode))
Next x

  MoveNode(n(7),-0.4,-0.4,-0.4, #PB_Absolute)
  MoveNode(n(8),-0.4,0.4,-0.4, #PB_Absolute)
  MoveNode(n(9),0.4,0.4,-0.4, #PB_Absolute)
  MoveNode(n(10),0.4,-0.4,-0.4, #PB_Absolute)
  
  MoveNode(n(5),-1.5,-0.1,-0.4, #PB_Absolute)
  MoveNode(n(6),1.5,-0.1,-0.4, #PB_Absolute)
  
  MoveNode(n(12),0,0.8,-2, #PB_Absolute)
  MoveNode(n(13),0,-0.8,-2, #PB_Absolute)
  
  MoveNode(n(1),-0.4,-0.4,0.4, #PB_Absolute)
  MoveNode(n(2),-0.4,0.4,0.4, #PB_Absolute)
  MoveNode(n(3),0.4,0.4,0.4, #PB_Absolute)
  MoveNode(n(4),0.4,-0.4,0.4, #PB_Absolute)
  
  MoveNode(n(0),0,0,1, #PB_Absolute)
  MoveNode(n(11),0,0,-1.5, #PB_Absolute)


  AttachNodeObject(mainnode, NodeID(n(0)))
  AttachNodeObject(mainnode, NodeID(n(1)))
  AttachNodeObject(mainnode, NodeID(n(2)))
  AttachNodeObject(mainnode, NodeID(n(3)))
  AttachNodeObject(mainnode, NodeID(n(4)))
  AttachNodeObject(mainnode, NodeID(n(5)))
  AttachNodeObject(mainnode, NodeID(n(6)))
  AttachNodeObject(mainnode, NodeID(n(7)))
  AttachNodeObject(mainnode, NodeID(n(8)))
  AttachNodeObject(mainnode, NodeID(n(9)))
  AttachNodeObject(mainnode, NodeID(n(10)))
  AttachNodeObject(mainnode, NodeID(n(11)))
  AttachNodeObject(mainnode, NodeID(n(12)))
  AttachNodeObject(mainnode, NodeID(n(13)))
  
EndProcedure


Procedure DrawWaveform_old()
  
StartDrawing(CanvasOutput(#GADGET_Canvas1))

Box(0, 0, 320, 150, $000000)
LineXY(0, 75, 320, 75, $0000FF)

If SoundFlag = 1
    For ti = 0 To NumNodes+2
    XpN = VertexArr(ti)\XVal
    YpN = VertexArr(ti)\YVal
    If ti>0
    LineXY(XpO, YpO, XpN, YpN, $F00F00)  
    EndIf
    XpO = XpN
    YpO = YpN
    Next ti  
  EndIf
  
StopDrawing()
                          
EndProcedure  


Procedure DrawWaveform()
  
  StartDrawing(CanvasOutput(#GADGET_Canvas1))
  
  Box(0, 0, 320, 150, $000000)
  LineXY(0, 75, 320, 75, $0000FF)
  
  If SoundFlag = 1
      For ti = 0 To 320
        XpN = ti
        
        exti.d=ti
      
      listPosi = (Int(exti/(320/(NewNumNodes+2))))+1
      
        wy1i.d = VertexArr(listPosi)\YVal
        ex1i.d = VertexArr(listPosi)\XVal
        wy0i.d = VertexArr(listPosi-1)\YVal
        ex0i.d = VertexArr(listPosi-1)\XVal
        mui.f = ((-Cos(((exti-ex0i)/(ex1i-ex0i))*#PI2/2))/2)+0.5
        wyti.f = ((wy1i-wy0i)*mui)+wy0i 
        
        YpN = wyti
        
        If ti>0
        LineXY(XpO, YpO, XpN, YpN, $00FF00)  
        EndIf
      
        XpO = XpN
        YpO = YpN
      Next ti  
      
  EndIf
    
StopDrawing()

EndProcedure


Procedure Tetrahedron()
  NumNodes = 3
  
NewNumNodes = (NumNodes*2)+1 

Dim VertexArr.VertexPos(NewNumNodes+2)  
Dim sampleArr.VertexPos(NewNumNodes+2) 
   
  Dim n(NumNodes)
 Protected a.f = 0.5*2.4
 
 Color1 = RGB($FF, $FF, $FF)

    CreateMesh(0, #PB_Mesh_LineStrip, #PB_Mesh_Static)
    
    MeshVertexPosition(a,a,a)
    MeshVertexColor(Color1)
    MeshVertexPosition(-a,a,-a)
    MeshVertexColor(Color1)
    MeshVertexPosition(a,-a,-a)
    MeshVertexColor(Color1)

    AddSubMesh(#PB_Mesh_LineStrip)
    MeshVertexPosition(-a,a, -a)
    MeshVertexColor(Color1)
    MeshVertexPosition(-a,-a,a)
    MeshVertexColor(Color1)
    MeshVertexPosition(a,-a,-a)
    MeshVertexColor(Color1)

    AddSubMesh(#PB_Mesh_LineStrip)
    MeshVertexPosition(a,a,a)
    MeshVertexColor(Color1)
    MeshVertexPosition(a,-a,-a)
    MeshVertexColor(Color1)
    MeshVertexPosition(-a,-a,a)
    MeshVertexColor(Color1)
    
    AddSubMesh(#PB_Mesh_LineStrip)
    MeshVertexPosition(a,a, a)
    MeshVertexColor(Color1)
    MeshVertexPosition(-a,-a,a)
    MeshVertexColor(Color1)
    MeshVertexPosition(-a,a,-a)
    MeshVertexColor(Color1)
    
    FinishMesh(#False) 
    
    CreateMaterial(0, LoadTexture(0, "White.jpg"))
    DisableMaterialLighting(0, #True)
    
    SetMeshMaterial(0, MaterialID(0))
    mainnode = CreateNode(#PB_Any,0, 0, 0)
    AttachNodeObject(mainnode, MeshID(0))
    
    For x.i = 0 To NumNodes : n(x) = CreateNode(#PB_Any) : Next x
    
    For x = 0 To NumNodes
    DetachNodeObject(n(x), NodeID(mainnode))
    Next x
  
  MoveNode(n(0),-a,a,-a, #PB_Absolute)
  MoveNode(n(1),a,-a,-a, #PB_Absolute)
  MoveNode(n(2),-a,-a,a, #PB_Absolute)
  MoveNode(n(3),a,a,a, #PB_Absolute)

  AttachNodeObject(mainnode, NodeID(n(0)))
  AttachNodeObject(mainnode, NodeID(n(1)))
  AttachNodeObject(mainnode, NodeID(n(2)))
  AttachNodeObject(mainnode, NodeID(n(3)))
  
  CameraZoom = 5.8
  MoveCamera(0, 0, 0, CameraZoom, #PB_Absolute)
  SetGadgetState(#Trackbar_Camera1, 580)

EndProcedure  


Procedure HexPrism()
  
  NumNodes = 11
  
  NewNumNodes = (NumNodes*2)+1 

  Dim VertexArr.VertexPos(NewNumNodes+2)  
  Dim sampleArr.VertexPos(NewNumNodes+2) 
  
  Dim n(NumNodes)
  
  Protected a.f = (Pow(3,0.5)/2)*VariableShapeParam1 
  Protected b.f = (0.5)*VariableShapeParam2
  Protected t.f = 0.7
 
  Color1 = RGB($FF, $FF, $FF)
  
 CreateMesh(0, #PB_Mesh_LineStrip, #PB_Mesh_Static)
    
    MeshVertexPosition(a*t,b*t,-b*t)
    MeshVertexColor(Color1)
    MeshVertexPosition(a*t,b*t,b*t)
    MeshVertexColor(Color1)
    MeshVertexPosition(a*t,-b*t,b*t)
    MeshVertexColor(Color1)
    MeshVertexPosition(a*t,-b*t,-b*t)
    MeshVertexColor(Color1)

    AddSubMesh(#PB_Mesh_LineStrip)
    MeshVertexPosition(0,1*t,b*t)
    MeshVertexColor(Color1)
    MeshVertexPosition(0,1*t,-b*t)
    MeshVertexColor(Color1)
    MeshVertexPosition(a*t,b*t,-b*t)
    MeshVertexColor(Color1)
    MeshVertexPosition(a*t,b*t,b*t)
    MeshVertexColor(Color1)

    AddSubMesh(#PB_Mesh_LineStrip)
    MeshVertexPosition(-a*t,b*t,-b*t)
    MeshVertexColor(Color1)
    MeshVertexPosition(-a*t,b*t,b*t)
    MeshVertexColor(Color1)
    MeshVertexPosition(0,1*t,b*t)
    MeshVertexColor(Color1)
    MeshVertexPosition(0,1*t,-b*t)
    MeshVertexColor(Color1)
    
    AddSubMesh(#PB_Mesh_LineStrip)
    MeshVertexPosition(-a*t,-b*t,b*t)
    MeshVertexColor(Color1)
    MeshVertexPosition(-a*t,-b*t,-b*t)
    MeshVertexColor(Color1)
    MeshVertexPosition(-a*t,b*t,-b*t)
    MeshVertexColor(Color1)
    MeshVertexPosition(-a*t,b*t,b*t)
    MeshVertexColor(Color1)
    
    AddSubMesh(#PB_Mesh_LineStrip)
    MeshVertexPosition(0,-1*t,b*t)
    MeshVertexColor(Color1)
    MeshVertexPosition(0,-1*t,-b*t)
    MeshVertexColor(Color1)
    MeshVertexPosition(-a*t,-b*t,-b*t)
    MeshVertexColor(Color1)
    MeshVertexPosition(-a*t,-b*t,b*t)
    MeshVertexColor(Color1)
    
    AddSubMesh(#PB_Mesh_LineStrip)
    MeshVertexPosition(0,-1*t,-b*t)
    MeshVertexColor(Color1)
    MeshVertexPosition(a*t,-b*t,-b*t)
    MeshVertexColor(Color1)
    MeshVertexPosition(a*t,-b*t,b*t)
    MeshVertexColor(Color1)
    MeshVertexPosition(0,-1*t,b*t)
    MeshVertexColor(Color1)
    
    AddSubMesh(#PB_Mesh_LineStrip)
    MeshVertexPosition(-a*t,b*t,b*t)
    MeshVertexColor(Color1)
    MeshVertexPosition(0,1*t,b*t)
    MeshVertexColor(color1)
    MeshVertexPosition(a*t,b*t,b*t)
    MeshVertexColor(color1)
    MeshVertexPosition(a*t,-b*t,b*t)
    MeshVertexColor(color1) 
    MeshVertexPosition(0,-1*t,b*t)
    MeshVertexColor(color1)
    MeshVertexPosition(-a*t,-b*t,b*t)
    MeshVertexColor(color1)
    MeshVertexPosition(-a*t,b*t,b*t)
    MeshVertexColor(Color1)
    
    AddSubMesh(#PB_Mesh_LineStrip)
    MeshVertexPosition(-a*t,b*t,-b*t)
    MeshVertexColor(Color1)
    MeshVertexPosition(0,1*t,-b*t)
    MeshVertexColor(Color1)
    MeshVertexPosition(a*t,b*t,-b*t)
    MeshVertexColor(Color1)
    MeshVertexPosition(a*t,-b*t,-b*t)
    MeshVertexColor(Color1) 
    MeshVertexPosition(0,-1*t,-b*t)
    MeshVertexColor(Color1)
    MeshVertexPosition(-a*t,-b*t,-b*t)
    MeshVertexColor(Color1)  

    FinishMesh(#False) 
    
    SetMeshMaterial(0, MaterialID(0))
    mainnode = CreateNode(#PB_Any,0, 0, 0)
    AttachNodeObject(mainnode, MeshID(0))
    
    For x.i = 0 To NumNodes : n(x) = CreateNode(#PB_Any) : Next x
    
    For x = 0 To NumNodes
    DetachNodeObject(n(x), NodeID(mainnode))
    Next x
  
    MoveNode(n(0),a*t,b*t,-b*t, #PB_Absolute)
    MoveNode(n(1),a*t,b*t,b*t, #PB_Absolute)
    MoveNode(n(2),a*t,-b*t,b*t, #PB_Absolute)
    MoveNode(n(3),a*t,-b*t,-b*t, #PB_Absolute)
    MoveNode(n(4),0,1*t,b*t, #PB_Absolute)
    MoveNode(n(5),0,1*t,-b*t, #PB_Absolute)
    MoveNode(n(6),-a*t,b*t,-b*t, #PB_Absolute)
    MoveNode(n(7),-a*t,b*t,b*t, #PB_Absolute)
    MoveNode(n(8),-a*t,-b*t,b*t, #PB_Absolute)
    MoveNode(n(9),-a*t,-b*t,-b*t, #PB_Absolute)
    MoveNode(n(10),0,-1*t,b*t, #PB_Absolute)
    MoveNode(n(11),0,-1*t,-b*t, #PB_Absolute)
  
    AttachNodeObject(mainnode, NodeID(n(0)))
    AttachNodeObject(mainnode, NodeID(n(1)))
    AttachNodeObject(mainnode, NodeID(n(2)))
    AttachNodeObject(mainnode, NodeID(n(3)))
    AttachNodeObject(mainnode, NodeID(n(4)))
    AttachNodeObject(mainnode, NodeID(n(5)))
    AttachNodeObject(mainnode, NodeID(n(6)))
    AttachNodeObject(mainnode, NodeID(n(7)))
    AttachNodeObject(mainnode, NodeID(n(8)))
    AttachNodeObject(mainnode, NodeID(n(9)))
    AttachNodeObject(mainnode, NodeID(n(10)))
    AttachNodeObject(mainnode, NodeID(n(11)))
    
EndProcedure  



Procedure Airplane()
  NumNodes = 13

  NewNumNodes = (NumNodes*2)+1 

  Dim VertexArr.VertexPos(NewNumNodes+2)  
  Dim sampleArr.VertexPos(NewNumNodes+2) 
  
  Dim n(NumNodes)
   
  Color1 = RGB($FF, $FF, $FF)

    CreateMesh(0, #PB_Mesh_LineStrip, #PB_Mesh_Static)
    
    ;Base
    MeshVertexPosition(-0.4,-0.4,-0.4)
    MeshVertexColor(Color1)
    MeshVertexPosition(-0.4,0.4,-0.4)
    MeshVertexColor(Color1)
    MeshVertexPosition(0.4,0.4,-0.4)
    MeshVertexColor(Color1)
    MeshVertexPosition(0.4,-0.4,-0.4)
    MeshVertexColor(Color1)
    MeshVertexPosition(-0.4,-0.4,-0.4) 
    MeshVertexColor(Color1)
    
    ;Apex 2
    AddSubMesh(#PB_Mesh_LineStrip)
    MeshVertexPosition(-0.4,-0.4,-0.4)
    MeshVertexColor(Color1)
    MeshVertexPosition(0,0,-1.5)
    MeshVertexColor(Color1)
    MeshVertexPosition(-0.4,0.4,-0.4)
    MeshVertexColor(Color1)
    MeshVertexPosition(0,0,-1.5)
    MeshVertexColor(Color1)
    MeshVertexPosition(0.4,0.4,-0.4)
    MeshVertexColor(Color1)
    MeshVertexPosition(0,0,-1.5)
    MeshVertexColor(Color1)
    MeshVertexPosition(0.4,-0.4,-0.4)
    MeshVertexColor(Color1)
    MeshVertexPosition(0,0,-1.5)
    MeshVertexColor(Color1)
    
    ;Top
    AddSubMesh(#PB_Mesh_LineStrip)
    MeshVertexPosition(-0.4,-0.4, 0.4)
    MeshVertexColor(Color1)
    MeshVertexPosition(-0.4,0.4, 0.4)
    MeshVertexColor(Color1)
    MeshVertexPosition(0.4,0.4, 0.4)
    MeshVertexColor(Color1)
    MeshVertexPosition(0.4,-0.4, 0.4)
    MeshVertexColor(Color1)
    MeshVertexPosition(-0.4,-0.4, 0.4) 
    MeshVertexColor(Color1)
    
    ;Wing1
    AddSubMesh(#PB_Mesh_LineStrip)
    MeshVertexPosition(-0.4,-0.4, 0.4)
    MeshVertexColor(Color1)
    MeshVertexPosition(-1.5,-0.1, -0.4)
    MeshVertexColor(Color1)
    MeshVertexPosition(-0.4,-0.4, -0.4)
    MeshVertexColor(Color1)
    
    ;Wing2
    AddSubMesh(#PB_Mesh_LineStrip)
    MeshVertexPosition(0.4,-0.4, 0.4)
    MeshVertexColor(Color1)
    MeshVertexPosition(1.5,-0.1, -0.4)
    MeshVertexColor(Color1)
    MeshVertexPosition(0.4,-0.4, -0.4)
    MeshVertexColor(Color1)
    
    ;Tail
    AddSubMesh(#PB_Mesh_LineStrip)
    MeshVertexPosition(0,0.4, -0.4)
    MeshVertexColor(Color1)
    MeshVertexPosition(0,0.8, -2)
    MeshVertexColor(Color1)
    MeshVertexPosition(0,0, -1.5)
    MeshVertexColor(Color1)
    MeshVertexPosition(0,-0.8, -2)
    MeshVertexColor(Color1)
    MeshVertexPosition(0,-0.4, -0.4)
    MeshVertexColor(Color1)
    
    ;Apex1
    AddSubMesh(#PB_Mesh_LineStrip)
    MeshVertexPosition(-0.4,-0.4, 0.4)
    MeshVertexColor(Color1)
    MeshVertexPosition(0,0, 1)
    MeshVertexColor(Color1)
    MeshVertexPosition(-0.4,0.4, 0.4)
    MeshVertexColor(Color1)
    MeshVertexPosition(0,0, 1)
    MeshVertexColor(Color1)
    MeshVertexPosition(0.4,0.4, 0.4)
    MeshVertexColor(Color1)
    MeshVertexPosition(0,0, 1)
    MeshVertexColor(Color1)
    MeshVertexPosition(0.4,-0.4, 0.4)
    MeshVertexColor(Color1)
    MeshVertexPosition(0,0, 1)
    MeshVertexColor(Color1)

    ;Connecter lines between top and base
    AddSubMesh(#PB_Mesh_LineStrip)
    MeshVertexPosition(-0.4,0.4,-0.4)
    MeshVertexColor(Color1)
    MeshVertexPosition(-0.4,0.4, 0.4)
    MeshVertexColor(Color1)
    
    AddSubMesh(#PB_Mesh_LineStrip)
    MeshVertexPosition(-0.4,-0.4,0.4)
    MeshVertexColor(Color1)
    MeshVertexPosition(-0.4,-0.4,-0.4)
    MeshVertexColor(Color1)
    
    AddSubMesh(#PB_Mesh_LineStrip)
    MeshVertexPosition(0.4,0.4,-0.4)
    MeshVertexColor(Color1)
    MeshVertexPosition(0.4,0.4,0.4)
    MeshVertexColor(Color1)
    
    AddSubMesh(#PB_Mesh_LineStrip)
    MeshVertexPosition(0.4,-0.4,-0.4)
    MeshVertexColor(Color1)
    MeshVertexPosition(0.4,-0.4,0.4)
    MeshVertexColor(Color1)
    
    FinishMesh(#False)
    
    CreateMaterial(0, LoadTexture(0, "White.jpg"))
    DisableMaterialLighting(0, #True)
    
    SetMeshMaterial(0, MaterialID(0))
    mainnode = CreateNode(#PB_Any,0, 0, 0)
    AttachNodeObject(mainnode, MeshID(0))
    
    For x.i = 0 To NumNodes : n(x) = CreateNode(#PB_Any) : Next x
    
    For x = 0 To NumNodes
    DetachNodeObject(n(x), NodeID(mainnode))
    Next x

  MoveNode(n(7),-0.4,-0.4,-0.4, #PB_Absolute)
  MoveNode(n(8),-0.4,0.4,-0.4, #PB_Absolute)
  MoveNode(n(9),0.4,0.4,-0.4, #PB_Absolute)
  MoveNode(n(10),0.4,-0.4,-0.4, #PB_Absolute)
  
  MoveNode(n(5),-1.5,-0.1,-0.4, #PB_Absolute)
  MoveNode(n(6),1.5,-0.1,-0.4, #PB_Absolute)
  
  MoveNode(n(12),0,0.8,-2, #PB_Absolute)
  MoveNode(n(13),0,-0.8,-2, #PB_Absolute)
  
  MoveNode(n(1),-0.4,-0.4,0.4, #PB_Absolute)
  MoveNode(n(2),-0.4,0.4,0.4, #PB_Absolute)
  MoveNode(n(3),0.4,0.4,0.4, #PB_Absolute)
  MoveNode(n(4),0.4,-0.4,0.4, #PB_Absolute)
  
  MoveNode(n(0),0,0,1, #PB_Absolute)
  MoveNode(n(11),0,0,-1.5, #PB_Absolute)


  AttachNodeObject(mainnode, NodeID(n(0)))
  AttachNodeObject(mainnode, NodeID(n(1)))
  AttachNodeObject(mainnode, NodeID(n(2)))
  AttachNodeObject(mainnode, NodeID(n(3)))
  AttachNodeObject(mainnode, NodeID(n(4)))
  AttachNodeObject(mainnode, NodeID(n(5)))
  AttachNodeObject(mainnode, NodeID(n(6)))
  AttachNodeObject(mainnode, NodeID(n(7)))
  AttachNodeObject(mainnode, NodeID(n(8)))
  AttachNodeObject(mainnode, NodeID(n(9)))
  AttachNodeObject(mainnode, NodeID(n(10)))
  AttachNodeObject(mainnode, NodeID(n(11)))
  AttachNodeObject(mainnode, NodeID(n(12)))
  AttachNodeObject(mainnode, NodeID(n(13)))
  
  CameraZoom = 5.8
  MoveCamera(0, 0, 0, CameraZoom, #PB_Absolute)
  SetGadgetState(#Trackbar_Camera1, 580)

EndProcedure  
Portaudio code:

Code: Select all

; /*
;  * $Id: portaudio.h 1083 2006-08-23 07:30:49Z rossb $
;  * PortAudio Portable Real-Time Audio Library
;  * PortAudio API Header File
;  * Latest version available at: http://www.portaudio.com/
;  *
;  * Copyright (c) 1999-2002 Ross Bencina And Phil Burk
;  *
;  * Permission is hereby granted, free of charge, To any person obtaining
;  * a copy of this software And associated documentation files
;  * (the "Software"), To deal in the Software without restriction,
;  * including without limitation the rights To use, copy, modify, merge,
;  * publish, distribute, sublicense, And/Or sell copies of the Software,
;  * And To permit persons To whom the Software is furnished To do so,
;  * subject To the following conditions:
;  *
;  * The above copyright notice And this permission notice shall be
;  * included in all copies Or substantial portions of the Software.
;  *
;  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;  * EXPRESS Or IMPLIED, INCLUDING BUT Not LIMITED To THE WARRANTIES OF
;  * MERCHANTABILITY, FITNESS For A PARTICULAR PURPOSE And NONINFRINGEMENT.
;  * IN NO EVENT SHALL THE AUTHORS Or COPYRIGHT HOLDERS BE LIABLE For
;  * ANY CLAIM, DAMAGES Or OTHER LIABILITY, WHETHER IN AN ACTION OF
;  * CONTRACT, TORT Or OTHERWISE, ARISING FROM, OUT OF Or IN CONNECTION
;  * With THE SOFTWARE Or THE USE Or OTHER DEALINGS IN THE SOFTWARE.
;  */
; 
; /*
;  * The text above constitutes the entire PortAudio license; however, 
;  * the PortAudio community also makes the following non-binding requests:
;  *
;  * Any person wishing To distribute modifications To the Software is
;  * requested To send the modifications To the original developer so that
;  * they can be incorporated into the canonical version. It is also 
;  * requested that these non-binding requests be included along With the 
;  * license above.
;  */


Enumeration ; PaErrorCode
  #paNoError = 0

  #paNotInitialized = -10000
  #paUnanticipatedHostError
  #paInvalidChannelCount
  #paInvalidSampleRate
  #paInvalidDevice
  #paInvalidFlag
  #paSampleFormatNotSupported
  #paBadIODeviceCombination
  #paInsufficientMemory
  #paBufferTooBig
  #paBufferTooSmall
  #paNullCallback
  #paBadStreamPtr
  #paTimedOut
  #paInternalError
  #paDeviceUnavailable
  #paIncompatibleHostApiSpecificStreamInfo
  #paStreamIsStopped
  #paStreamIsNotStopped
  #paInputOverflowed
  #paOutputUnderflowed
  #paHostApiNotFound
  #paInvalidHostApi
  #paCanNotReadFromACallbackStream      ;/**< @todo review error code name */
  #paCanNotWriteToACallbackStream       ;/**< @todo review error code name */
  #paCanNotReadFromAnOutputOnlyStream   ;/**< @todo review error code name */
  #paCanNotWriteToAnInputOnlyStream     ;/**< @todo review error code name */
  #paIncompatibleStreamHostApi
  #paBadBufferPtr
EndEnumeration

#paNoDevice = (-1)
#paUseHostApiSpecificDeviceSpecification = (-2)

Enumeration ; PaHostApiTypeId
  #paInDevelopment=0 ; /* use While developing support For a new host API */
  #paDirectSound=1
  #paMME=2
  #paASIO=3
  #paSoundManager=4
  #paCoreAudio=5
  #paOSS=7
  #paALSA=8
  #paAL=9
  #paBeOS=10
  #paWDMKS=11
  #paJACK=12
  #paWASAPI=13
EndEnumeration

#paFloat32        = ($00000001)
#paInt32          = ($00000002)
#paInt24          = ($00000004)
#paInt16          = ($00000008)
#paInt8           = ($00000010)
#paUInt8          = ($00000020)
#paCustomFormat   = ($00010000)
#paNonInterleaved = ($80000000)

#paFormatIsSupported = (0)
#paFramesPerBufferUnspecified = (0)
#paNoFlag          = (0)
#paClipOff         = ($00000001)
#paDitherOff       = ($00000002)
#paNeverDropInput  = ($00000004)
#paPrimeOutputBuffersUsingStreamCallback = ($00000008)
#paPlatformSpecificFlags = ($FFFF0000)

#paInputUnderflow   = ($00000001)
#paInputOverflow    = ($00000002)
#paOutputUnderflow  = ($00000004)
#paOutputOverflow   = ($00000008)
#paPrimingOutput    = ($00000010)

Enumeration ; PaStreamCallbackResult
  #paContinue=0
  #paComplete=1
  #paAbort=2
EndEnumeration

Structure PaHostApiInfo
  structVersion.l
  type.l
 *name
  deviceCount.l
  defaultInputDevice.l
  defaultOutputDevice.l
EndStructure

Structure PaHostErrorInfo
  hostApiType.l   
  errorCode.l            
 *errorText        
EndStructure

Structure PaDeviceInfo
  structVersion.l
 *name
  hostApi.l
  maxInputChannels.l
  maxOutputChannels.l
  defaultLowInputLatency.d
  defaultLowOutputLatency.d
  defaultHighInputLatency.d
  defaultHighOutputLatency.d
  defaultSampleRate.d
EndStructure

Structure PaStreamParameters
  device.l
  channelCount.l
  sampleFormat.l
  suggestedLatency.d
 *hostApiSpecificStreamInfo
EndStructure

Structure PaStreamCallbackTimeInfo
  inputBufferAdcTime.d
  currentTime.d
  outputBufferDacTime.d
EndStructure

Structure PaStreamInfo
  structVersion.l
  inputLatency.d
  outputLatency.d
  sampleRate.d
EndStructure

PrototypeC PaStreamCallback(*input, *output, frameCount, *timeInfo, statusFlags, *userdata)
PrototypeC PaStreamFinishedCallback(*user_data)

ImportC "portaudio_x86.lib"
  Pa_GetVersion()
  Pa_GetVersionText()
  Pa_GetErrorText(errorCode)
  Pa_Initialize()
  Pa_Terminate()
  Pa_GetHostApiCount()
  Pa_GetDefaultHostApi()
  Pa_GetHostApiInfo(hostApi)
  Pa_HostApiTypeIdToHostApiIndex(type)
  Pa_HostApiDeviceIndexToDeviceIndex(hostApi, hostApiDeviceIndex)
  Pa_GetLastHostErrorInfo()
  Pa_GetDeviceCount()
  Pa_GetDefaultInputDevice()
  Pa_GetDefaultOutputDevice()
  Pa_GetDeviceInfo(device)
  Pa_IsFormatSupported(*inputParameters, *outputParameters, sampleRate.d)
  Pa_OpenStream(*stream.LONG, *inputParameters, *outputParameters, sampleRate.d, framesPerBuffer, streamFlags, *streamCallback, *user_data)
  Pa_OpenDefaultStream(*stream.LONG, numInputChannels, numOutputChannels, sampleFormat, sampleRate.d, framesPerBuffer, *streamCallback, *user_data)
  Pa_CloseStream(*stream)
  Pa_SetStreamFinishedCallback(*stream, *streamFinishedCallback)
  Pa_StartStream(*stream)
  Pa_StopStream(*stream)
  Pa_AbortStream(*stream)
  Pa_IsStreamStopped(*stream)
  Pa_IsStreamActive(*stream)
  Pa_GetStreamInfo(*stream)
  Pa_GetStreamTime.d(*stream)
  Pa_GetStreamCpuLoad.d(*stream)
  Pa_ReadStream(*stream, *buffer, frames)
  Pa_WriteStream(*stream, *buffer, frames)
  Pa_GetStreamReadAvailable(*stream)
  Pa_GetStreamWriteAvailable(*stream)
  Pa_GetSampleSize(format)
  Pa_Sleep(msec)
  PaAsio_ShowControlPanel(deviceIndex, systemSpecific)
EndImport
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Rotatosound

Post by Kukulkan »

Cool 8) I just checked the video and it sounds interesting. Maybe someday there is a VSTi from it ;-)
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: Rotatosound

Post by applePi »

Thank you doctornash for the nice demo
at first i was not able to find a suitable portaudio_x86.lib + portaudio_x86.dll for the code to run. but then find these 2 files in your package in https://www.mediafire.com/file/wjtmqlbn ... r_v101.zip
viewtopic.php?f=14&t=69760
and then it works.
will be useful in a future projects such as changing the mesh according to the sound vibrations, frequency and amplitude, something like in example SetMeshData.pb in the PB examples
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Rotatosound

Post by Kwai chang caine »

Works well here W10 X64 / v5.62 / x86
Thanks for sharing and also to ApplePI for his tips :wink:
ImageThe happiness is a road...
Not a destination
Post Reply