Click here to Skip to main content
12,510,189 members (51,370 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

6.6K views
2 bookmarked
Posted

Other Applications of Bitwise Operations

, 23 Sep 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Harmonizing music scales

Introduction

This tip presents two simple applications of bit-wise operators: the "Imp" operator and the "And" operator.

The first function uses the Imp operator to find chord types whose notes are a subset of a given musical scale:

Function ReturnChordNumbers(ParentScaleNumber As Integer) As Integer()
   Dim n As Integer
   Dim mChordNumbers() As Integer, index As Integer
   
   For n = 2048 To 4095
   
      If (n Imp ParentScaleNumber) = -1 Then
         index = index + 1
         ReDim Preserve mChordNumbers(1 To index)
         mChordNumbers(index) = n
      End If
      
   Next n
   
   ReturnChordNumbers = mChordNumbers
   Erase mChordNumbers
End Function    

The second function does the reverse process and returns the scale types (or "larger" chord types) that contain all the notes of a specified chord. It uses the And operator.

Function ReturnScaleNumbers(DecimalChordNumber As Integer) As Integer()
   Dim n As Integer
   Dim mScaleNumbers() As Integer, index As Integer
   
   For n = 2048 To 4095
      If (n And DecimalChordNumber) = DecimalChordNumber Then
         index = index + 1
         ReDim Preserve mScaleNumbers(1 To index)
         mScaleNumbers(index) = n
      End If
   Next n
   ReturnScaleNumbers = mScaleNumbers
End Function   

In the functions written above, integer variable n has a range of 2048 to 4095, which represents all combinations of root formulas that can be extracted from the 12 intervals of the chromatic scale formula.

Here are some examples of common music scales that could be harmonized using the first function,

while here are some widely used chord types that could be entered into the second function:

Both functions would require a decimal value as the input and can be called by adding supporting procedures such as those written below:

Private Sub Command1_Click()
Dim mArray() As Integer
Dim index As Integer

mArray = ReturnChordNumbers(txtInput.Text)

List1.Clear
For index = LBound(mArray) To UBound(mArray)
   List1.AddItem mArray(index)
Next index
End Sub
Private Sub Command2_Click()
Dim mArray() As Integer

Dim index As Integer

mArray = ReturnScaleNumbers(txtInput.Text)
List1.Clear
For index = LBound(mArray) To UBound(mArray)
   List1.AddItem mArray(index)
Next index
End Sub

' // Another alternative to the 1st function. 
' // If we were to use the "Or" operator, the same output would result.

Function ReturnChordNumbers_1B(ParentScaleNumber As Integer) As Integer()
   Dim n As Integer
   Dim mChordNumbers() As Integer, index As Integer
   
   For n = 2048 To 4095
   
      If (ParentScaleNumber Or n) = ParentScaleNumber Then
'// also: (n Or ParentScaleNumber) = ParentScaleNumber
         index = index + 1
         ReDim Preserve mChordNumbers(1 To index)
         mChordNumbers(index) = n
      End If
      
   Next n
   
   ReturnChordNumbers_1B = mChordNumbers
   Erase mChordNumbers
End Function

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ha_asgag
Philippines Philippines
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160929.1 | Last Updated 23 Sep 2012
Article Copyright 2012 by ha_asgag
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid