הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Friday, September 18, 2009 7:46 PM
My program turns on and off external relays from the parallel port.
I need to find a way to activate the relay only while a wav file is playing and
de-activate the relay when the wav file stops playing.
Can I read the timecode from the wave file, and use it to accomplish this task?
If this is possible, please provide a sample of the code that I would need to use.
Is there a flag that gets set when a wav file is playing that I can query?
I am calling up my wav files using the following code:
My.Computer.Audio.Play(SoundFilePath & "s1.wav")
Thank you in advance for your help,
Allan
All replies (5)
Saturday, September 19, 2009 8:15 AM ✅Answered
I have been playing around with some primitive proof of concept code and I believe it should work, but I have a few questions:
Is the method for extracting and using the time-code information from mp3 files just as simple as the song.duration that is used in the directX code below?
Which method is the best for my needs, directX or MP3 timecode?
How is the MP3 time-code read from VB?
Here is what I have started so far
Imports Microsoft.DirectX
Imports Microsoft.DirectX.AudioVideoPlayback
Public Class Form1
Dim song As Audio
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'song = New Audio("c:\Real.mp3", True)
If song Is Nothing Then
MsgBox("No song selected." & vbCrLf & "Please select a song and try again", MsgBoxStyle.Critical, "ERROR")
Else
song.Play()
Timer1.Enabled = True
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'If song.Playing = True Then song.Stop()
Dim openDLG As OpenFileDialog = New OpenFileDialog
openDLG.Filter = _
"Audio Media (*.mid, *.mp3, *.wav, *.wma|*.mid;*.mp3;*.wav;*.wma"
If openDLG.ShowDialog = Windows.Forms.DialogResult.OK Then
If song Is Nothing Then
song = New Audio(openDLG.FileName, False)
Label1.Text = CInt(song.Duration)
Label2.Text = openDLG.FileName
ProgressBar1.Maximum = song.Duration
TrackBar1.Maximum = song.Duration
Else
If song.Playing = True Then song.Stop()
song = New Audio(openDLG.FileName, False)
'Dim x As Integer = song.Duration
Label1.Text = song.Duration
Label2.Text = openDLG.FileName
ProgressBar1.Maximum = song.Duration
TrackBar1.Maximum = song.Duration
End If
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If song Is Nothing Then
'DO NOTHING
Exit Sub
Else
song.Stop()
Timer1.Enabled = False
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If song.Playing = True Then
Label1.Text = song.CurrentPosition
ProgressBar1.Value = song.CurrentPosition
TrackBar1.Value = song.CurrentPosition
End If
End Sub
Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
song.CurrentPosition = TrackBar1.Value
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
'Dim time As Integer = TimeSpan.Parse(CInt(song.Duration)).Minutes
'Label3.Text = time
If song.Duration >= 60 Then
Label3.Text = CInt(song.Duration) / 60
End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
End Class
EDIT: This method works perfectly for my requirements! I have spent all day testing the code with my relay control box and its
working flawlessly. I added a few tests on the time-code and my relays trigger when the song starts and un-engage when the song finishes.
DirectX is surprisingly easy to use, and is probably the simplest method to get the job done, the KISS rule definitely applies here.
Friday, September 18, 2009 9:07 PM
Wave files do not carry any time information that I know of. (I am referrring to ADPCM format.)
You can tell how long a wave file is by analyzing the WAVEFORMATEX header, and determining the number of samples in the file and the sample period. The total time of the file is the number of samples * sample period. Sample period is *1 / samples per second.
*I have some files that might be useful to you, but they are at home, and my Internet connection is down now. Hopefully, I can fix it and get you a sample of what I am talking about.
This is best done with the Multimedia API, as opposed to using VB exclusively. (I have the code for using MM API in VB, too.)
http://msdn.microsoft.com/en-us/library/dd370784(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms716686(VS.85).aspx
Friday, September 18, 2009 9:12 PM
jinzai,
Thank you for your reply.
I will look the the links you provided, and I look forward to seeing a sample from you.
Someone mentioned that directX uses the timecode information in wav files but I
have never used directX before.
Friday, September 18, 2009 9:32 PM
The problem is that WAV format does not provide time code inherently. In files with time code (WAV extension), the time is "recorded" as an extra channel. That's somewhat silly, since the method I am referring to does not require a separate channel for time. Naturally, pausing will cause synchronization errors, but...if you are in charge of pausing, then it should pose no real problem.
SMPTE is used in AVI files, and it is a bona fide timecode format, whereas anything disguised as an audio channel is a hack by comparison. (Not a problem, just not standard operating procedure for WAV files.) SMPTE is also overkill for audio files, its intent is to sync a video stream with an audio stream. (Otherwise, videos look like poorly dubbed foreign language films, or a Milli Vanilli concert.)
mp3 contain timecode as part of the decoding...its entirely necessary in that case because the audio is being manufactured algorithmically by your computer. WAVE files, by comparison...as simply D/A converted as is on a strictly ordered basis. (i.e. Synchronization is the job of your sound card, and not your computer.)
DirectSound (DirectX) actually stuffs the primary buffer of the sound card, and it [probably] keeps track of the time in order to write to the buffer at the correct time, and in the correct place. (The audio buffers in DX are circular, and you must lock the buffer to write to it...its messy.)
I have some DX audio code, too. Okay, I'm bugging out from work...hopefully, I'll have something for you tomorrow, at least.
Saturday, September 19, 2009 1:55 AM
jinzai,
I have given it some thought and mp3 would be a great idea as long as they are as easy to use as wav files, in addition to the fact that they are much smaller (I like that) :)
At this point, either the mp3 or directX is going to be my best bet, in terms of simplicity.
Thank you again for taking the time to help me with this.
Allan