Implement the ILMADetUserCallback2 user callback to receive notifications of sound and silence detection. Register the callback by setting the ILMADet::CallbackObj2 property. The interface has one method, which reports sound and silence events. It is called by the filter.
DirectShow determines the duration of the audio buffer. However, if you use the LEADTOOLS Multimedia toolkit, you can change the size of the audio buffers using the get_AudioBufferSize property.
This interface should be used for C++, .NET or other programming languages that can cast a LONGLONG (__int64) value to a pointer.
See the following .NET examples (C# and VB.NET) for a class implementing this interface.
This function is called each time an audio sample is received by the filter.
nEvent |
Event notification type that represents whether the event is sound or silence. Possible values for are:
|
||||||||||
pData. |
Pointer to the audio data cast as a 64-bit LONGLONG. The sample data is an array of unsigned byte (8-bit) or signed short (16-bit) values. This value should be cast to a pointer (which may be an unsigned byte or a signed short, depending on the value of lBitsPerSample). |
||||||||||
lDataCount |
The number of elements in pData. If lBitsPerSample is 8, then this also represents the size (in bytes) of the buffer pointed to by Data. If lBitsPerSample is 16, then the size of the pData buffer is lDataCount * 2. |
||||||||||
lBitsPerSample |
Number of bits per sample of mono data. Values can be 8 or 16. This determines the format of the data pointed to by pData. |
||||||||||
lChannels |
Number of channels (i.e. 1=mono, 2=stereo...) |
||||||||||
lSamplesPerSec |
Sample rate, in samples per second. |
||||||||||
lAvgBytesPerSec. |
Average bandwidth in bytes per second. It should be equal to lSamplesPerSec * lChannels * (lBitsPerSample / 8). |
The filter calls this method whenever sound or silence is detected. Every time the callback is received, you are also given the audio data buffer.
S_OK if successful, an HRESULT error code otherwise.
Format of audio data:
The general format of the audio data (lChannels) is as follows:
Sample 0
Channel 0 |
Channel 1 |
- |
Channel lChannels - 1 |
The format for stereo data is simple: there are two values per sample (one for the left channel and the other for the right channel):
pData(0) is Sample 0, Left channel
pData(1) is Sample 0, Right channel
pData(2) is Sample 1, Left channel
pData(3) is Sample 1, Right channel
The format of the data is different, depending on whether lBitsPerChannel is 8 or 16.
The format for mono data is simple, because there is only one channel. In such cases, each value in the array is one sample:
pData(0) is Sample 0
pData(1) is Sample 1
The format for stereo data is still simple: there are two values per sample (one for left channel and the other for right channel):
pData(0) is Sample 0, Left channel
pData(1) is Sample 0, Right channel
pData(2) is Sample 1, Left channel
pData(3) is Sample 1, Right channel
Format of 8-bit audio data:
The values in the array are unsigned and between 0 and 255. The real audio value is obtained by subtracting 128 from the array value and should have a value between -128 and 127. When changing the value, remember to add 128 before putting the data back.
Format of 16-bit data:
The values in the array are signed and between -32768 and 32767. They contain the real audio values. It is not necessary to perform conversions like those for the 8-bit data.
Examples
Example 1: Fast C# code (using unsafe code)
This C# code silences the sound track by setting all sound values to 0. Note that this sample uses pointers, which means your application must be compiled as using unsafe code. For slower, safe code, look at the second example, which is written in VB.NET.
private void Form1_Load(object sender, EventArgs e)
{
string file = @"c:\myfile.avi";
// Insert Audio Detection Filter
LTMMLib.ltmmProcessor proc = GetAudioProcessor("LEAD Audio Detection Filter (2.0)");
playControl.SelectedAudioProcessors.Add(proc, 0);
// Hook callback.
LMAAudioDetLib.ILMADet AudioDet = (LMAAudioDetLib.ILMADet)playControl.GetSubObject((int)LTMMLib.ltmmPlay_Object.ltmmPlay_Object_SelAudioProcessor);
AudioDet.CallbackObj2 = new AudioDetectionCallback();
// Play it.
playControl.sourcefile = file;
}
public class AudioDetectionCallback : LMAAudioDetLib.ILMADetUserCallback2
{
unsafe public void CallbackProc(int nEvent, long pData, int lDataCount, int lBitsPerSample, int lChannels, int lSamplesPerSec, int lAvgBytesPerSec)
{
IntPtr ptrData = new IntPtr(pData);
int i;
if (lBitsPerPixel == 16)
{
short* ptrData16 = (short*)ptrData.ToPointer();
for(i = 0; i < lDataCount; i++)
ptrData16[i] = 0;
}
else
{
byte* ptrData8 = (byte*)ptrData.ToPointer();
for(i = 0; i < lDataCount; i++)
ptrData8[i] = 0;
}
}
}
Example 2: VB.NET code (slower, but using safe code)
This VB.NET code silences the sound track by setting all the sound values to 0. Note that this sample is safe and does not use pointers. It is slower than the unsafe code above.
Public Class VBAudioDetectionCallback : Implements LMAAudioDetLib.ILMADetUserCallback2
Public Sub New()
End Sub
Sub CallbackProc(ByVal nEvent As Integer, ByVal pData As Long, ByVal lDataCount As Integer, ByVal lBitsPerSample As Integer, ByVal lChannels As Integer, ByVal lSamplesPerSec As Integer, ByVal lAvgBytesPerSec As Integer) Implements LMAAudioDetLib.ILMADetUserCallback2.CallbackProc
Dim ptrData As IntPtr = New IntPtr(pData)
Dim i As Integer
' copy the data to a managed array, since VB does not support pointers.
If lBitsPerSample = 16 Then
Dim ptrData16(lDataCount) As Short
' Copy data from pData into ptrData16
System.Runtime.InteropServices.Marshal.Copy(ptrData, ptrData16, 0, lDataCount)
' set the data to 0
For i = 0 To lDataCount - 1
ptrData16(i) = 0
Next
' copy the data back into ptrData
System.Runtime.InteropServices.Marshal.Copy(ptrData16, 0, ptrData, lDataCount)
Else
Dim ptrData8(lDataCount) As Byte
' Copy data from pData into ptrData8
System.Runtime.InteropServices.Marshal.Copy(ptrData, ptrData8, 0, lDataCount)
' set the data to 0
For i = 0 To lDataCount - 1
ptrData8(i) = 0
Next
' copy the data back into ptrData
System.Runtime.InteropServices.Marshal.Copy(ptrData8, 0, ptrData, lDataCount)
End If
End Sub
End Class