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
C# / VB.NET - [Visual Basic 2008] Leggere il volume master del computer
Forum - C# / VB.NET - [Visual Basic 2008] Leggere il volume master del computer

Pagine: [ 1 2 3 ] Precedente | Prossimo
Avatar
Furion (Normal User)
Rookie


Messaggi: 51
Iscritto: 31/01/2008

Segnala al moderatore
Postato alle 17:47
Venerdì, 24/04/2009
Ciao raga, torno a rompervi le scatole. Dovrei fare un programmino per un mio amico che gli visualizzi su schermo il volume master in percentuale. Per farlo, ho trovato online una classe già fatta in grado di darmi le informazioni che volevo. Il problema sta nel fatto che questa classe "dice" di avere 2 eventi: MuteChanged e VolumeChanged che, in teoria, dovrebbero attivarsi quando cambia il volume o lo status di muto. In realtà ciò non avviene e per rilevare queste modifiche, quindi, ho costruito un ciclo infinito che ad ogni istante mi confronta il volume attuale con quello dell'istante precedente. Sapevo già che questa sarebbe stata una soluzione pesantissima, ma mi sono accorto che una volta lanciato questo ciclo infinito, ad ogni iterazione il mio processo aumenta la sua dimensione in memoria, perchè la classe sonora alloca nuova memoria ogni volta che acquisisce il volume corrente e lo stato di muto. All'interno della mia classe (quella del mio progetto) ho utilizzato questo codice per creare un'istanza della classe sonora
Codice sorgente - presumibilmente VB.NET

  1. Dim WithEvents Snd As New Sound



Questo, invece, è il ciclo infinito
Codice sorgente - presumibilmente VB.NET

  1. ' Controlla gli eventi finchè non si chiude il form
  2. While keepListening
  3.    ' Mantiene l'applicazione aggiornata
  4.    Application.DoEvents()
  5.    ' Calcola il volume attuale
  6.    Dim nowVol As Int32 = Snd.GetVolume()
  7.    ' Se è cambiato il volume
  8.    If volume <> nowVol Then
  9.       volumeChanged(nowVol)
  10.    End If
  11.    ' Se è cambiato lo status di muto
  12.    If mute <> Snd.GetMuted Then
  13.       muteChanged()
  14.    End If
  15. End While



mentre queste sono le signature dei metodi volumeChanged() e muteChanged()
Codice sorgente - presumibilmente C# / VB.NET

  1. Private Sub volumeChanged(ByVal newVol As Int32)


Codice sorgente - presumibilmente C# / VB.NET

  1. Private Sub muteChanged()



All'inizio ad entrambi i metodi non passavo nessun parametro e alla fine delle loro sig. avevo scritto, rispettivamente, "Handles Snd.VolumeChanged" e "Handles Snd.MuteChanged". Infine, questo è il codice della fantomatica classe sonora.

Codice sorgente - presumibilmente VB.NET

  1. Imports System
  2. Imports System.Runtime.InteropServices
  3.  
  4. Public Class Sound
  5.  
  6. #Region "   Events"
  7.     Public Event MuteChanged()
  8.     Public Event VolumeChanged()
  9. #End Region
  10.  
  11. #Region "   Constants"
  12.     Private Const MMSYSERR_NOERROR As Integer = 0
  13.     Private Const MAXPNAMELEN As Integer = 32
  14.     Private Const MIXER_LONG_NAME_CHARS As Integer = 64
  15.     Private Const MIXER_SHORT_NAME_CHARS As Integer = 16
  16.     Private Const MIXER_GETLINEINFOF_COMPONENTTYPE As Integer = &H3
  17.     Private Const MIXER_GETLINECONTROLSF_ONEBYTYPE As Integer = &H2
  18.     Private Const MIXER_GETCONTROLDETAILSF_VALUE As Integer = &H0
  19.     Private Const MIXER_SETCONTROLDETAILSF_VALUE As Integer = &H0
  20.     Private Const MIXERLINE_COMPONENTTYPE_DST_FIRST As Integer = &H0
  21.     Private Const MIXERLINE_COMPONENTTYPE_DST_SPEAKERS As Integer = MIXERLINE_COMPONENTTYPE_DST_FIRST + 4
  22.     'Private Const MIXERLINE_COMPONENTTYPE_SRC_FIRST As Integer = &H1000
  23.     'Private Const MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE As Integer = MIXERLINE_COMPONENTTYPE_SRC_FIRST + 3
  24.     'Private Const MIXERLINE_COMPONENTTYPE_SRC_LINE As Integer = MIXERLINE_COMPONENTTYPE_SRC_FIRST + 2
  25.     'Private Const MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT = (MIXERLINE_COMPONENTTYPE_SRC_FIRST + 8)
  26.     Private Const MIXERCONTROL_CT_CLASS_FADER As Integer = &H50000000
  27.     Private Const MIXERCONTROL_CT_UNITS_UNSIGNED As Integer = &H30000
  28.     Private Const MIXERCONTROL_CT_CLASS_SWITCH As Integer = &H20000000
  29.     Private Const MIXERCONTROL_CT_UNITS_BOOLEAN As Integer = &H10000
  30.     Private Const MIXERCONTROL_CONTROLTYPE_BASS As Integer = (MIXERCONTROL_CONTROLTYPE_FADER + 2)
  31.     Private Const MIXERCONTROL_CONTROLTYPE_FADER As Integer = MIXERCONTROL_CT_CLASS_FADER Or MIXERCONTROL_CT_UNITS_UNSIGNED
  32.     Private Const MIXERCONTROL_CONTROLTYPE_VOLUME As Integer = MIXERCONTROL_CONTROLTYPE_FADER + 1
  33.     Private Const MIXERCONTROL_CONTROLTYPE_MUTE As Integer = (MIXERCONTROL_CONTROLTYPE_BOOLEAN + 2)
  34.     Private Const MIXERCONTROL_CONTROLTYPE_TREBLE As Integer = (MIXERCONTROL_CONTROLTYPE_FADER + 3)
  35.     Private Const MIXERCONTROL_CONTROLTYPE_EQUALIZER As Integer = (MIXERCONTROL_CONTROLTYPE_FADER + 4)
  36.     Private Const MIXERCONTROL_CONTROLTYPE_BOOLEAN As Integer = (MIXERCONTROL_CT_CLASS_SWITCH Or MIXERCONTROL_CT_UNITS_BOOLEAN)
  37. #End Region
  38.  
  39. #Region "   API Calls"
  40.     Private Declare Ansi Function mixerClose Lib "winmm.dll" (ByVal hmx As Integer) As Integer
  41.     Private Declare Ansi Function mixerGetControlDetailsA Lib "winmm.dll" (ByVal hmxobj As Integer, ByRef pmxcd As MIXERCONTROLDETAILS, ByVal fdwDetails As Integer) As Integer
  42.     Private Declare Ansi Function mixerGetDevCapsA Lib "winmm.dll" (ByVal uMxId As Integer, ByVal pmxcaps As MIXERCAPS, ByVal cbmxcaps As Integer) As Integer
  43.     Private Declare Ansi Function mixerGetID Lib "winmm.dll" (ByVal hmxobj As Integer, ByVal pumxID As Integer, ByVal fdwId As Integer) As Integer
  44.     Private Declare Ansi Function mixerGetLineControlsA Lib "winmm.dll" (ByVal hmxobj As Integer, ByRef pmxlc As MIXERLINECONTROLS, ByVal fdwControls As Integer) As Integer
  45.     Private Declare Ansi Function mixerGetLineInfoA Lib "winmm.dll" (ByVal hmxobj As Integer, ByRef pmxl As MIXERLINE, ByVal fdwInfo As Integer) As Integer
  46.     Private Declare Ansi Function mixerGetNumDevs Lib "winmm.dll" () As Integer
  47.     Private Declare Ansi Function mixerMessage Lib "winmm.dll" (ByVal hmx As Integer, ByVal uMsg As Integer, ByVal dwParam1 As Integer, ByVal dwParam2 As Integer) As Integer
  48.     Private Declare Ansi Function mixerOpen Lib "winmm.dll" (ByRef phmx As Integer, ByVal uMxId As Integer, ByVal dwCallback As Integer, ByVal dwInstance As Integer, ByVal fdwOpen As Integer) As Integer
  49.     Private Declare Ansi Function mixerSetControlDetails Lib "winmm.dll" (ByVal hmxobj As Integer, ByRef pmxcd As MIXERCONTROLDETAILS, ByVal fdwDetails As Integer) As Integer
  50.     Private Declare Function mixerGetLineInfo Lib "winmm.dll" Alias "mixerGetLineInfoA" (<MarshalAs(UnmanagedType.I4)> ByVal hmxobj As Integer, ByRef pmxl As MIXERLINE, ByVal fdwInfo As Integer) As Integer
  51.     Private Declare Function mixerGetLineControls Lib "winmm.dll" Alias "mixerGetLineControlsA" (<MarshalAs(UnmanagedType.I4)> ByVal hmxobj As Integer, ByRef pmxlc As MIXERLINECONTROLS, ByVal fdwControls As Integer) As Integer
  52. #End Region
  53.  
  54. #Region "   Structures"
  55.     Private Structure MIXERCAPS
  56.         Public wMid As Integer
  57.         Public wPid As Integer
  58.         Public vDriverVersion As Integer
  59.         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAXPNAMELEN)> Public szPname As String
  60.         Public fdwSupport As Integer
  61.         Public cDestinations As Integer
  62.     End Structure 'MIXERCAPS
  63.        _
  64.     <StructLayout(LayoutKind.Sequential)> _
  65.     Private Structure MIXERCONTROL
  66.         <FieldOffset(0)> Public cbStruct As Integer           '  size in Byte of MIXERCONTROL
  67.         <FieldOffset(4)> Public dwControlID As Integer        '  unique control id for mixer device
  68.         <FieldOffset(8)> Public dwControlType As Integer      '  MIXERCONTROL_CONTROLTYPE_xxx
  69.         <FieldOffset(12)> Public fdwControl As Integer         '  MIXERCONTROL_CONTROLF_xxx
  70.         <FieldOffset(16)> Public cMultipleItems As Integer     '  if MIXERCONTROL_CONTROLF_MULTIPLE set
  71.         <FieldOffset(20), MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=MIXER_SHORT_NAME_CHARS)> Public szShortName As String ' * MIXER_SHORT_NAME_CHARS  ' short name of control
  72.         <FieldOffset(36), MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=MIXER_LONG_NAME_CHARS)> Public szName As String '  * MIXER_LONG_NAME_CHARS ' Integer name of control
  73.         <FieldOffset(100)> Public lMinimum As Integer           '  Minimum value
  74.         <FieldOffset(104)> Public lMaximum As Integer           '  Maximum value
  75.         <FieldOffset(108), MarshalAs(UnmanagedType.ByValArray, SizeConst:=11, ArraySubType:=UnmanagedType.AsAny)> Public reserved() As Integer      '  reserved structure space
  76.     End Structure
  77.  
  78.     <StructLayout(LayoutKind.Sequential)> _
  79.     Private Structure MIXERCONTROLDETAILS
  80.         <FieldOffset(0)> Public cbStruct As Integer       '  size in Byte of MIXERCONTROLDETAILS
  81.         <FieldOffset(4)> Public dwControlID As Integer    '  control id to get/set details on
  82.         <FieldOffset(8)> Public cChannels As Integer      '  number of channels in paDetails array
  83.         <FieldOffset(12)> Public item As Integer           '  hwndOwner or cMultipleItems
  84.         <FieldOffset(16)> Public cbDetails As Integer      '  size of _one_ details_XX struct
  85.         <FieldOffset(20)> Public paDetails As IntPtr       '  pointer to array of details_XX structs
  86.     End Structure
  87.  
  88.     <StructLayout(LayoutKind.Sequential)> _
  89.     Private Structure MIXERCONTROLDETAILS_UNSIGNED
  90.         <FieldOffset(0)> Public dwValue As Integer        '  value of the control
  91.     End Structure
  92.  
  93.     <StructLayout(LayoutKind.Sequential)> _
  94.     Private Structure MIXERLINE
  95.         <FieldOffset(0)> Public cbStruct As Integer                '  size of MIXERLINE structure
  96.         <FieldOffset(4)> Public dwDestination As Integer          '  zero based destination index
  97.         <FieldOffset(8)> Public dwSource As Integer               '  zero based source index (if source)
  98.         <FieldOffset(12)> Public dwLineID As Integer               '  unique line id for mixer device
  99.         <FieldOffset(16)> Public fdwLine As Integer                '  state/information about line
  100.         <FieldOffset(20)> Public dwUser As Integer                 '  driver specific information
  101.         <FieldOffset(24)> Public dwComponentType As Integer        '  component type line connects to
  102.         <FieldOffset(28)> Public cChannels As Integer              '  number of channels line supports
  103.         <FieldOffset(32)> Public cConnections As Integer           '  number of connections (possible)
  104.         <FieldOffset(36)> Public cControls As Integer              '  number of controls at this line
  105.         <FieldOffset(40), MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=MIXER_SHORT_NAME_CHARS)> Public szShortName As String  ' * MIXER_SHORT_NAME_CHARS
  106.         <FieldOffset(56), MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=MIXER_LONG_NAME_CHARS)> Public szName As String ' * MIXER_LONG_NAME_CHARS
  107.         <FieldOffset(120)> Public dwType As Integer
  108.         <FieldOffset(124)> Public dwDeviceID As Integer
  109.         <FieldOffset(128)> Public wMid As Integer
  110.         <FieldOffset(132)> Public wPid As Integer
  111.         <FieldOffset(136)> Public vDriverVersion As Integer
  112.         <FieldOffset(168), MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=MAXPNAMELEN)> Public szPname As String ' * MAXPNAMELEN
  113.     End Structure
  114.  
  115.     <StructLayout(LayoutKind.Sequential)> _
  116.     Private Structure MIXERLINECONTROLS
  117.         <FieldOffset(0)> Public cbStruct As Integer       '  size in Byte of MIXERLINECONTROLS
  118.         <FieldOffset(4)> Public dwLineID As Integer       '  line id (from MIXERLINE.dwLineID)
  119.         <FieldOffset(8)> Public dwControl As Integer      '  MIXER_GETLINECONTROLSF_ONEBYTYPE
  120.         <FieldOffset(12)> Public cControls As Integer      '  count of controls pmxctrl points to
  121.         <FieldOffset(16)> Public cbmxctrl As Integer       '  size in Byte of _one_ MIXERCONTROL
  122.         <FieldOffset(20)> Public pamxctrl As IntPtr       '  pointer to first MIXERCONTROL array
  123.     End Structure
  124.  
  125. #End Region
  126.  
  127.     Private Shared Function GetVolumeControl(ByVal hmixer As Integer, ByVal componentType As Integer, ByVal ctrlType As Integer, ByRef mxc As MIXERCONTROL, ByRef vCurrentVol As Integer) As Boolean
  128.         Dim mxlc As New MIXERLINECONTROLS
  129.         Dim mxl As New MIXERLINE
  130.         Dim pmxcd As New MIXERCONTROLDETAILS
  131.         Dim du As New MIXERCONTROLDETAILS_UNSIGNED
  132.         mxc = New MIXERCONTROL
  133.         Dim rc As Integer
  134.         Dim retValue As Boolean
  135.         vCurrentVol = -1
  136.  
  137.         mxl.cbStruct = Marshal.SizeOf(mxl)
  138.         mxl.dwComponentType = componentType
  139.  
  140.         rc = mixerGetLineInfoA(hmixer, mxl, MIXER_GETLINEINFOF_COMPONENTTYPE)
  141.  
  142.         If MMSYSERR_NOERROR = rc Then
  143.             Dim sizeofMIXERCONTROL As Integer = 152
  144.             Dim ctrl As Integer = Marshal.SizeOf(GetType(MIXERCONTROL))
  145.             mxlc.pamxctrl = Marshal.AllocCoTaskMem(sizeofMIXERCONTROL)
  146.             mxlc.cbStruct = Marshal.SizeOf(mxlc)
  147.             mxlc.dwLineID = mxl.dwLineID
  148.             mxlc.dwControl = ctrlType
  149.             mxlc.cControls = 1
  150.             mxlc.cbmxctrl = sizeofMIXERCONTROL
  151.  
  152.             ' Allocate a buffer for the control
  153.             mxc.cbStruct = sizeofMIXERCONTROL
  154.  
  155.             ' Get the control
  156.             rc = mixerGetLineControlsA(hmixer, mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE)
  157.  
  158.             If MMSYSERR_NOERROR = rc Then
  159.                 retValue = True
  160.  
  161.                 ' Copy the control into the destination structure
  162.                 mxc = CType(Marshal.PtrToStructure(mxlc.pamxctrl, GetType(MIXERCONTROL)), MIXERCONTROL)
  163.             Else
  164.                 retValue = False
  165.             End If
  166.             Dim sizeofMIXERCONTROLDETAILS As Integer = Marshal.SizeOf(GetType(MIXERCONTROLDETAILS))
  167.             Dim sizeofMIXERCONTROLDETAILS_UNSIGNED As Integer = Marshal.SizeOf(GetType(MIXERCONTROLDETAILS_UNSIGNED))
  168.             pmxcd.cbStruct = sizeofMIXERCONTROLDETAILS
  169.             pmxcd.dwControlID = mxc.dwControlID
  170.             pmxcd.paDetails = Marshal.AllocCoTaskMem(sizeofMIXERCONTROLDETAILS_UNSIGNED)
  171.             pmxcd.cChannels = 1
  172.             pmxcd.item = 0
  173.             pmxcd.cbDetails = sizeofMIXERCONTROLDETAILS_UNSIGNED
  174.  
  175.             rc = mixerGetControlDetailsA(hmixer, pmxcd, MIXER_GETCONTROLDETAILSF_VALUE)
  176.  
  177.             du = Marshal.PtrToStructure(pmxcd.paDetails, GetType(MIXERCONTROLDETAILS_UNSIGNED))
  178.  
  179.             vCurrentVol = du.dwValue
  180.  
  181.             Return retValue
  182.         End If
  183.         retValue = False
  184.         Return retValue
  185.     End Function 'GetVolumeControl
  186.  
  187.     Private Shared Function SetVolumeControl(ByVal hmixer As Integer, ByVal mxc As MIXERCONTROL, ByVal volume As Integer) As Boolean
  188.         ' This function sets the value for a volume control.
  189.         ' Returns True if successful
  190.         Dim retValue As Boolean
  191.         Dim rc As Integer
  192.         Dim mxcd As New MIXERCONTROLDETAILS
  193.         Dim vol As New MIXERCONTROLDETAILS_UNSIGNED
  194.  
  195.         mxcd.item = 0
  196.         mxcd.dwControlID = mxc.dwControlID
  197.         mxcd.cbStruct = Marshal.SizeOf(mxcd)
  198.         mxcd.cbDetails = Marshal.SizeOf(vol)
  199.  
  200.         ' Allocate a buffer for the control value buffer
  201.         mxcd.cChannels = 1
  202.         vol.dwValue = volume
  203.  
  204.         ' Copy the data into the control value buffer
  205.         mxcd.paDetails = Marshal.AllocCoTaskMem(Marshal.SizeOf(GetType(MIXERCONTROLDETAILS_UNSIGNED)))
  206.         Marshal.StructureToPtr(vol, mxcd.paDetails, False)
  207.  
  208.         ' Set the control value
  209.         rc = mixerSetControlDetails(hmixer, mxcd, MIXER_SETCONTROLDETAILSF_VALUE)
  210.  
  211.         If MMSYSERR_NOERROR = rc Then
  212.             retValue = True
  213.         Else
  214.             retValue = False
  215.         End If
  216.         Return retValue
  217.     End Function 'SetVolumeControl
  218.  
  219.     Public Function GetVolume() As Integer
  220.         Dim mixer As Integer
  221.         Dim volCtrl As New MIXERCONTROL
  222.         Dim currentVol As Integer
  223.         mixerOpen(mixer, 0, 0, 0, 0) 'Returns the mixer control
  224.         GetVolumeControl(mixer, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, MIXERCONTROL_CONTROLTYPE_VOLUME, volCtrl, currentVol)
  225.         mixerClose(mixer)
  226.         Return currentVol
  227.     End Function 'GetVolume
  228.     Public Function GetMax() As Integer
  229.         Dim mixer As Integer
  230.         Dim volCtrl As New MIXERCONTROL
  231.         Dim currentVol As Integer
  232.         mixerOpen(mixer, 0, 0, 0, 0)
  233.         Dim type As Integer = MIXERCONTROL_CONTROLTYPE_VOLUME
  234.         GetVolumeControl(mixer, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, type, volCtrl, currentVol)
  235.         mixerClose(mixer)
  236.         Return volCtrl.lMaximum
  237.     End Function 'Gets max volume
  238.     Public Function GetMuted() As String
  239.         Dim mixer As Integer
  240.         Dim volCtrl As New MIXERCONTROL
  241.         Dim Muted As Integer
  242.         mixerOpen(mixer, 0, 0, 0, 0)
  243.         Dim type As Integer = MIXERCONTROL_CONTROLTYPE_MUTE
  244.         GetVolumeControl(mixer, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, type, volCtrl, Muted)
  245.         mixerClose(mixer)
  246.         Return Muted
  247.     End Function 'GetMuted status
  248.  
  249.     Public Sub SetVolume(ByVal vVolume As Integer)
  250.         Dim mixer As Integer
  251.         Dim volCtrl As New MIXERCONTROL
  252.         Dim currentVol As Integer
  253.         mixerOpen(mixer, 0, 0, 0, 0)
  254.         Dim type As Integer = MIXERCONTROL_CONTROLTYPE_VOLUME
  255.         GetVolumeControl(mixer, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, type, volCtrl, currentVol)
  256.         If vVolume > volCtrl.lMaximum Then
  257.             vVolume = volCtrl.lMaximum
  258.         End If
  259.         If vVolume < volCtrl.lMinimum Then
  260.             vVolume = volCtrl.lMinimum
  261.         End If
  262.         SetVolumeControl(mixer, volCtrl, vVolume)
  263.         GetVolumeControl(mixer, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, type, volCtrl, currentVol)
  264.         If vVolume <> currentVol Then
  265.             Throw New Exception("Cannot Set Volume")
  266.         Else
  267.             RaiseEvent VolumeChanged()
  268.         End If
  269.         mixerClose(mixer)
  270.     End Sub 'SetVolume
  271.  
  272.     Public Sub SetMuted(ByVal boolMute As Boolean)
  273.         ' This routine sets the volume setting of the current unit depending on the value passed through
  274.         Dim mixer As Integer
  275.         Dim volCtrl As New MIXERCONTROL
  276.         Dim lngReturn As Integer
  277.         Dim type As Integer = MIXERCONTROL_CONTROLTYPE_MUTE
  278.         Dim currentVol As Integer
  279.         ' Obtain the hmixer struct
  280.         lngReturn = mixerOpen(mixer, 0, 0, 0, 0)
  281.         ' Error check
  282.         If lngReturn <> 0 Then Exit Sub
  283.         ' Obtain the volumne control object
  284.         GetVolumeControl(mixer, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, type, volCtrl, currentVol)
  285.         ' Then set the volume
  286.         SetVolumeControl(mixer, volCtrl, boolMute)
  287.         mixerClose(mixer)
  288.         RaiseEvent MuteChanged()
  289.     End Sub 'Set the muted status
  290.  
  291. End Class



EDIT: scusate il poema ma non sono riuscito a mettere il codice della classe Sound come allegato :-|

Ultima modifica effettuata da Furion il 25/04/2009 alle 10:29
PM Quote
Avatar
Il Totem (Admin)
Guru^2


Messaggi: 3635
Iscritto: 24/01/2006

Segnala al moderatore
Postato alle 10:35
Sabato, 25/04/2009
Metti il controllo in un timer, o in un thread diverso con una pausa di qualche decina (o centinaia) di millisecondi. Una frequenza di refresh di 10-20Hz mi sembra già più accettabile, che non il ciclo infinito. Poi puoi provare a chiamare GC.Collect() per forzare una garbage collection della memoria (operazione, comunque, altamente sconsigliata).
Se posso fare un commento, mi sembrano inutili gli eventi esposti in quella classe quando per generarli è necessaria una chiamata esplicita ad un metodo d'istanza.

PM Quote
Avatar
Furion (Normal User)
Rookie


Messaggi: 51
Iscritto: 31/01/2008

Segnala al moderatore
Postato alle 11:38
Sabato, 25/04/2009
Grazie per i consigli Totem. Infatti anche a me quegli eventi messi così lasciano perplesso. Il problema non sono tanto gli eventi, quanto il fatto che ogni volta che uso le funzioni getVolume e getMuted mi si alloca memoria aggiuntiva e non so come ripulirla. Grazie ancora, comunque ^_^

PM Quote
Avatar
Furion (Normal User)
Rookie


Messaggi: 51
Iscritto: 31/01/2008

Segnala al moderatore
Postato alle 18:13
Martedì, 28/04/2009
Raga, scusate il doppio post ma sono ancora in alto mare. Con il consiglio di Totem la velocità con cui il mio processo aumenta di dimensione in ram è calata di molto, ma il problema persiste (e quindi io consulto il medico XD). Ho notato, se può essere utile, che la dimensione del processo adesso cresce con una velocità di circa 4 kb/sec e che la colpa dovrebbe essere quasi esclusivamente della funzione GetVolumeControl(). Aiutatemi vi prego:d

PM Quote
Avatar
Il Totem (Admin)
Guru^2


Messaggi: 3635
Iscritto: 24/01/2006

Segnala al moderatore
Postato alle 10:01
Mercoledì, 29/04/2009
Puoi al limite inizializzare le strutture usate all'interno di quella funzione nel costruttore New e dichiararle come variabili a livello di classe. Questo processo evita la creazione di nuove istanze ad ogni controllo... Tuttavia le variabili temporanee dovrebbero essere distrutte a fine procedura e non capisco perchè questo non avvenga.

PM Quote
Avatar
theprogrammer (Normal User)
Guru^2


Messaggi: 2509
Iscritto: 28/01/2009

Segnala al moderatore
Postato alle 10:30
Mercoledì, 29/04/2009
Nella GetVolumeControl vedo che usi due volte la Marshal.AllocCoTaskMem ma non usi mai le corrispondenti Marshal.FreeCoTaskMem quando la memoria non ti serve piu'.

PM Quote
Avatar
Furion (Normal User)
Rookie


Messaggi: 51
Iscritto: 31/01/2008

Segnala al moderatore
Postato alle 11:11
Mercoledì, 29/04/2009
Bhe la classe non l'ho scritta io e come potete facilmente immaginare le mie abilità di programmatore vb net non sono così approfondite. Potete rispiegarmi bene cosa devo fare? In pratica devo far diventare le variabili locali di classe e devo usare quel comando che citava theProgrammer per deallocare la memoria? Se funziona vi faccio una statua ^____^

PM Quote
Avatar
Thejuster (Admin)
Guru^2


Messaggi: 2305
Iscritto: 04/05/2008

Segnala al moderatore
Postato alle 18:21
Mercoledì, 29/04/2009
non fai prima ad usare le DirectAudio?

oltre a gestire il livello del volume master in modo facilmente
puoi anche tenere sott'occhio anche il volume di tutti i suoni che potrai riprodurre.
e applicare effetti come echo, reverb e tante altre cose.

evitando tutto quel poema.



https://mire.forumfree.it/ - Mire Engine
C# UI Designer
PM Quote
Avatar
Furion (Normal User)
Rookie


Messaggi: 51
Iscritto: 31/01/2008

Segnala al moderatore
Postato alle 19:46
Mercoledì, 29/04/2009
Con DirectAudio intendi le DirectX Audio? E come dovrei fare? Scusate ma in questo campo purtroppo sono un n0bb0ne. Potresti postarmi un esempio anche piccolino? Grazie in anticipo.

PM Quote
Pagine: [ 1 2 3 ] Precedente | Prossimo