Android Code Snippet midiDeltaFromLong (delay midicode from time)

Discussion in 'Code Snippets' started by Hans- Joachim Krahe, Nov 25, 2018.

  1. Hans- Joachim Krahe

    Hans- Joachim Krahe Active Member Licensed User

    Long Way to Tipperary...

    Code:
    private Sub midiDeltaFromLong (ticks As Long) As Byte()
       
        
    Dim int1, int2, int3,int4 As Int
        
    Dim bitstr As String = Bit.ToBinaryString(ticks) ' works with long as well...
        Dim bitbyte() As Byte =  bitstr.GetBytes("utf8"' BitStr nach bytes ("1","0" = 49,48)
        Dim bitbyte2(bitbyte.Length) As Byte            ' reihenfolge umkehren turn around
        For i = 0 To bitbyte.Length - 1
            bitbyte2(i) = bitbyte(bitbyte.Length - i - 
    1)
        
    Next
        
    Log("-------------------------------------")
        
    Log("Ticks:" & ticks)
        
    If  ticks < 128 Then
            
    ' 1 byte
            For i = 0 To bitbyte.Length - 1
                
    If bitbyte2(i)= 49 Then
                    int1 = int1  +  
    Power2,i)
                
    End If
            
    Next
            
    Dim b(1As Byte
            b(
    0) = int1
            
    Return b
        
    else If ticks < 16384 Then
            
    ' 2 byte
            For i = 0 To 6
                
    If bitbyte2(i)= 49 Then
                    int1 = int1  +  
    Power2,i)
                
    End If
            
    Next
            
    For i = 7 To bitbyte.Length - 1
                
    If bitbyte2(i)= 49 Then
                    int2 = int2  +  
    Power2,(i-7))
                
    End If
            
    Next
            int2 = int2  + 
    0x80
            
    Dim b(2As Byte
            b(
    0) = int2
            b(
    1) = int1
            
    Return b
        
    Else If ticks < 2097152 Then
            
    ' 3 byte
            For i = 0 To 6
                
    If bitbyte2(i)= 49 Then
                    int1 = int1  +  
    Power2,i)
                
    End If
            
    Next
            
    For i = 7 To 13
                
    If bitbyte2(i)= 49 Then
                    int2 = int2  +  
    Power2,(i-7))
                
    End If
            
    Next
            int2 = int2  + 
    0x80
            
    For i = 14 To bitbyte.Length - 1
                
    If bitbyte2(i)= 49 Then
                    int3 =int3+  
    Power2,(i-14))
                
    End If
            
    Next
            int3 = int3 +  
    0x80
            
    Dim b(3As Byte
            b(
    0) = int3
            b(
    1) = int2
            b(
    2)= int1
            
    Return b
        
    Else If ticks < 268435456 Then
            
    ' 4 byte
            For i = 0 To 6
                
    If bitbyte2(i)= 49 Then
                    int1 = int1  +  
    Power2,i)
                
    End If
            
    Next
            
    For i = 7 To 13
                
    If bitbyte2(i)= 49 Then
                    int2 = int2  +  
    Power2,(i-7))
                
    End If
            
    Next
            int2 = int2  + 
    0x80
            
    For i = 14 To 20
                
    If bitbyte2(i)= 49 Then
                    int3 =int3 +  
    Power2,(i-14))
                
    End If
            
    Next
            int3 = int3  + 
    0x80
            
    For i = 21 To bitbyte.Length - 1
                
    If bitbyte2(i)= 49 Then
                    int4 = int4  +  
    Power2,(i-21))
                
    End If
            
    Next
            int4 = int4  + 
    0x80
            
    Dim b(4As Byte
            b(
    0) = int4
            b(
    1) = int3
            b(
    2)= int2
            b(
    3)= int1
            
    Return b
        
    Else
            
    Dim b() As Byte
            b = bc.StringToBytes( 
    "hex:" & Bit.ToHexString(int4) & " " & Bit.ToHexString(int3) & " " & Bit.ToHexString(int2) & " " & Bit.ToHexString(int1),"utf8")
            
    Return b
        
    End If
    End Sub

    https://www.mobilefish.com/tutorials/midi/midi_quickguide_specification.html#figure_1
     
    Last edited: Nov 25, 2018
    Erel likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice