Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Sounder - Form1.vb

Form1.vb

Caricato da: Totem
Scarica il programma completo

  1. Public Class frmSounder
  2. #Region "Enumeratori / Strutture"
  3.     'Nome delle note
  4.     Public Enum NomeNota
  5.         _Do = -9
  6.         Re = -7
  7.         Mi = -5
  8.         Fa = -4
  9.         Sol = -2
  10.         La = 0
  11.         Si = 2
  12.         Pausa = -32000
  13.     End Enum
  14.     'Nome delle alterazioni
  15.     Public Enum Alterazione
  16.         Doppio_Dieses = 2
  17.         Diesis = 1
  18.         Bequadro = 0
  19.         Bemolle = -1
  20.         Doppio_Bemolle = -2
  21.     End Enum
  22.     'Nome delle ottave
  23.     Public Enum Ottava
  24.         Prima = -36
  25.         Seconda = -24
  26.         Terza = -12
  27.         Centrale = 0
  28.         Quinta = 12
  29.         Sesta = 24
  30.         Settima = 36
  31.     End Enum
  32.     'Nomi delle figure musicali, in funzione di una fusa
  33.     Public Enum Figura
  34.         Breve = 256
  35.         Semibreve = 128
  36.         Minima = 64
  37.         Semiminima = 32
  38.         Croma = 16
  39.         Semicroma = 8
  40.         Biscroma = 4
  41.         Semibiscroma = 2
  42.         Fusa = 1
  43.     End Enum
  44.     'Indicazione di tempo
  45.     Public Structure Tempo
  46.         Dim Fig As Figura
  47.         Dim Battiti As Byte
  48.         Sub New(ByVal F As Figura, ByVal b As Byte)
  49.             Fig = F
  50.             Battiti = b
  51.         End Sub
  52.         Shadows Function ToString() As String
  53.             Return Fig.ToString + " = " + Battiti.ToString
  54.         End Function
  55.     End Structure
  56.     'Una nota
  57.     Public Structure Nota
  58.         Dim Nome As NomeNota
  59.         Dim Alt As Alterazione
  60.         Dim Ott As Ottava
  61.         Dim Fig As Figura
  62.         Dim Punti As Byte
  63.         Sub New(ByVal n As NomeNota, ByVal a As Alterazione, ByVal o As Ottava, ByVal f As Figura, ByVal p As Byte)
  64.             Nome = n
  65.             Alt = a
  66.             Ott = o
  67.             Fig = f
  68.             Punti = p
  69.         End Sub
  70.         Shadows Function ToString() As String
  71.             Return Nome.ToString.Replace("_", "") + ToStr(Alt)
  72.         End Function
  73.         Function GetArrayName() As String()
  74.             Dim S() As String = { _
  75.             Nome.ToString.Replace("_", "") + ToStr(Alt), _
  76.             Fig.ToString + " " + New String(".", Punti), _
  77.             Ott.ToString}
  78.             Return S
  79.         End Function
  80.         'Restituisce il coefficiente di moltiplicazione della lunghezza di una nota con un dato numero di punti
  81.         Public Function GetCoef() As Single
  82.             Select Case Punti
  83.                 Case 0
  84.                     Return 1
  85.                 Case 1
  86.                     Return 1.5
  87.                 Case 2
  88.                     Return 1.75
  89.                 Case 3
  90.                     Return 1.975
  91.             End Select
  92.         End Function
  93.         'Frequenza di una nota, in hertz
  94.         Public Function GetFreq() As Single
  95.             If Nome = NomeNota.Pausa Then
  96.                 Return 0
  97.             End If
  98.             Dim C As Single
  99.             C = (Nome + Alt + Ott) / 12
  100.             Return 440 * (2 ^ C)
  101.         End Function
  102.         'Lunghezza di una nota, in millisecondi
  103.         Public Function GetLength(ByVal T As Tempo) As Single
  104.             Dim C As Single = 60000 / T.Battiti
  105.             Return C * (Fig * GetCoef()) / T.Fig
  106.         End Function
  107.         'Suona la nota
  108.         Public Sub Play()
  109.             Dim F, L As Int32
  110.             F = GetFreq()
  111.             L = GetLength(Time)
  112.             Beep(F, L)
  113.         End Sub
  114.     End Structure
  115. #End Region
  116. #Region "Metodi"
  117.     Declare Function Beep Lib "kernel32.dll" (ByVal dwFreq As Integer, ByVal dwDuration As Integer) As Boolean
  118.     Public Shared Function ToStr(ByVal Alt As Alterazione) As String
  119.         Select Case Alt
  120.             Case Alterazione.Bemolle
  121.                 Return "b"
  122.             Case Alterazione.Bequadro
  123.                 Return ""
  124.             Case Alterazione.Diesis
  125.                 Return "#"
  126.             Case Alterazione.Doppio_Bemolle
  127.                 Return "bb"
  128.             Case Alterazione.Doppio_Dieses
  129.                 Return "##"
  130.         End Select
  131.         Return ""
  132.     End Function
  133.     Function GetObject() As ListViewItem
  134.         Dim L As ListViewItem
  135.         If tabInsert.SelectedIndex = 0 Then
  136.             Dim N As New Nota
  137.             Select Case cmbNome.SelectedItem
  138.                 Case "Do"
  139.                     N.Nome = NomeNota._Do
  140.                 Case "Re"
  141.                     N.Nome = NomeNota.Re
  142.                 Case "Mi"
  143.                     N.Nome = NomeNota.Mi
  144.                 Case "Fa"
  145.                     N.Nome = NomeNota.Fa
  146.                 Case "Sol"
  147.                     N.Nome = NomeNota.Sol
  148.                 Case "La"
  149.                     N.Nome = NomeNota.La
  150.                 Case "Si"
  151.                     N.Nome = NomeNota.Si
  152.                 Case "Pausa"
  153.                     N.Nome = NomeNota.Pausa
  154.             End Select
  155.             N.Alt = 2 - cmbAlt.SelectedIndex
  156.             N.Ott = (cmbOtt.SelectedIndex - 3) * 12
  157.             N.Punti = nudPunti.Value
  158.             N.Fig = 2 ^ (8 - cmbFig.SelectedIndex)
  159.             L = New ListViewItem(N.GetArrayName)
  160.             L.Tag = N
  161.         Else
  162.             Dim T As New Tempo
  163.             T.Fig = 2 ^ (8 - cmbTimeFig.SelectedIndex)
  164.             T.Battiti = nudBattiti.Value
  165.             L = New ListViewItem(T.ToString)
  166.             L.Tag = T
  167.         End If
  168.         Return L
  169.     End Function
  170. #End Region
  171. #Region "Variabili globali / Costanti"
  172.     Public Shared Time As New Tempo(Figura.Semiminima, 120)
  173. #End Region
  174.     Private Sub frmSounder_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  175.        
  176.     End Sub
  177.     Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
  178.         lstNote.Items.Add(GetObject)
  179.     End Sub
  180.     Private Sub cmdInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdInsert.Click
  181.         If lstNote.SelectedIndices.Count > 0 Then
  182.             lstNote.Items.Insert(lstNote.SelectedIndices(0), GetObject)
  183.         Else
  184.             MsgBox("Selezionare un elemento!", MsgBoxStyle.Exclamation)
  185.         End If
  186.     End Sub
  187.     Private Sub cmdChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdChange.Click
  188.         If lstNote.SelectedIndices.Count > 0 Then
  189.             Dim L As ListViewItem = GetObject()
  190.             lstNote.Items(lstNote.SelectedIndices(0)).Text = L.Text
  191.             lstNote.Items(lstNote.SelectedIndices(0)).Tag = L.Tag
  192.         Else
  193.             MsgBox("Selezionare un elemento!", MsgBoxStyle.Exclamation)
  194.         End If
  195.     End Sub
  196.     Private Sub cmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemove.Click
  197.         If lstNote.SelectedIndices.Count > 0 Then
  198.             lstNote.Items.RemoveAt(lstNote.SelectedIndices(0))
  199.         Else
  200.             MsgBox("Selezionare un elemento!", MsgBoxStyle.Exclamation)
  201.         End If
  202.     End Sub
  203.     Private Sub cmdPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPlay.Click
  204.         Dim L As ListViewItem
  205.         Dim N As Nota
  206.         Time = New Tempo(Figura.Semiminima, 120)
  207.         For I As Int32 = 0 To lstNote.Items.Count - 1
  208.             L = lstNote.Items(I)
  209.             If TypeOf L.Tag Is Nota Then
  210.                 N = L.Tag
  211.                 N.Play()
  212.             Else
  213.                 Time = L.Tag
  214.             End If
  215.         Next
  216.     End Sub
  217.     Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click
  218.         lstNote.Items.Clear()
  219.     End Sub
  220.     Private Sub cmdBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBrowseSave.Click
  221.         Dim S As New SaveFileDialog
  222.         S.Filter = "File di notazione|*.mus"
  223.         If S.ShowDialog = Windows.Forms.DialogResult.OK Then
  224.             txtDirSave.Text = S.FileName
  225.         End If
  226.     End Sub
  227.     Private Sub cmdBrowseLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBrowseLoad.Click
  228.         Dim O As New OpenFileDialog
  229.         O.Filter = "File di notazione|*.mus"
  230.         If O.ShowDialog = Windows.Forms.DialogResult.OK Then
  231.             txtDirLoad.Text = O.FileName
  232.         End If
  233.     End Sub
  234.     Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
  235.         Try
  236.             Dim W As New IO.StreamWriter(txtDirSave.Text)
  237.             Dim L As ListViewItem
  238.             Dim N As Nota
  239.             Dim T As Tempo
  240.             Dim S As String
  241.  
  242.             For I As Int32 = 0 To lstNote.Items.Count - 1
  243.                 L = lstNote.Items(I)
  244.                 If TypeOf L.Tag Is Nota Then
  245.                     N = L.Tag
  246.                     S = N.Nome & "|" & N.Alt & "|" & N.Ott & "|" & N.Fig & "|" & N.Punti
  247.                 Else
  248.                     Time = L.Tag
  249.                     T = L.Tag
  250.                     S = T.Fig & "|" & T.Battiti
  251.                 End If
  252.                 W.Write(S + ";")
  253.                 Application.DoEvents()
  254.             Next
  255.             W.Close()
  256.             MsgBox("File salvato!", MsgBoxStyle.Information)
  257.         Catch Ex As Exception
  258.             MsgBox("Impossibile salvare il file!", MsgBoxStyle.Exclamation)
  259.         End Try
  260.     End Sub
  261.     Private Sub cmdLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoad.Click
  262.         Try
  263.             Dim R As New IO.StreamReader(txtDirLoad.Text)
  264.             Dim L As ListViewItem
  265.             Dim N As Nota
  266.             Dim T As Tempo
  267.             Dim S, All(), Tag() As String
  268.  
  269.             S = R.ReadToEnd
  270.             R.Close()
  271.  
  272.             All = S.Split(";")
  273.             For I As Int32 = 0 To UBound(All)
  274.                 If All(I) = "" Then
  275.                     Continue For
  276.                 End If
  277.                 Tag = All(I).Split("|")
  278.                 If Tag.Length = 5 Then
  279.                     N = New Nota(CInt(Tag(0)), CInt(Tag(1)), CInt(Tag(2)), CInt(Tag(3)), CInt(Tag(4)))
  280.                     L = New ListViewItem(N.GetArrayName)
  281.                     L.Tag = N
  282.                 Else
  283.                     T = New Tempo(CInt(Tag(0)), CInt(Tag(1)))
  284.                     L = New ListViewItem(T.ToString)
  285.                     L.Tag = T
  286.                 End If
  287.                 lstNote.Items.Add(L)
  288.                 Application.DoEvents()
  289.             Next
  290.         Catch Ex As Exception
  291.             MsgBox("Impossibile caricare il file!", MsgBoxStyle.Exclamation)
  292.         End Try
  293.     End Sub
  294. End Class