Results 1 to 13 of 13

Thread: [RESOLVED] How to play sound from memory using mciSendString API

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Resolved [RESOLVED] How to play sound from memory using mciSendString API

    VB 6

    I know how to play sounds from reading a .wav file but I need to be able to play sounds from memory. I want to load a wave file into a byte array and then be able to play from that array using mciSendString.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  2. #2
    Fanatic Member namrekka's Avatar
    Join Date
    Feb 2005
    Location
    Netherlands
    Posts
    639

    Re: How to play sound from memory using mciSendString API


  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to play sound from memory using mciSendString API

    @namrekka

    I haven't tried it out yet but I see, although it is from the winmm.dll, I see that it does not use mciSendString explicitly so I am wondering if it has the same return messages as mciSendString API. I have a current project that is using mciSendString and in the code it returns info that I need to perform a certain feature as it is playing a sound file.

    Here is a code snippet from that project that I require:

    Code:
    Dim Multimedia As MMedia
    Dim sngLength As Single
      '
      '
    Private Sub Timer1_Timer()
     Dim sngPosition As Single
     Dim sngPercent As Single
     Dim intX As Integer
    
     sngPosition = Multimedia.Position
        
     Label1.Caption = "Status: " & Multimedia.Status
        
     sngPercent = sngPosition / sngLength
     intX = (PicChannel.ScaleWidth - Screen.TwipsPerPixelX) * sngPercent
     
     L = Pic1Left + intX
     W = Pic1Width - intX
     
    Picture1.Move L, Picture1.Top, W, Picture1.Height
      '
      '
      '
    End Sub
    And in the Class Module I have:

    Code:
      '
      '
    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
     (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _
      ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
      '
      '
    Public Sub mmPlay()
     '   Plays the currently open file from the current position
     Dim lngReturn As Long   '   Hold the value returned by the mciSendString command
    
     '   If there is no file open then exit the sub
     If strAlias = "" Then
       Exit Sub
     End If
        
     If blnWait Then
       lngReturn = mciSendString("Play " & strAlias & " wait", "", 0, 0)
     Else
       lngReturn = mciSendString("Play " & strAlias, "", 0, 0)
     End If
    End Sub
      '
      '
    Public Property Get Position() As Single
     '   Returns the current position in the file
     Dim lngReturn As Long       '   Hold the value returned by the mciSendString
     Dim intLength As Integer
     Dim strPosition As String * 255     '   Holds the returned length from the mci
                                             '   status call
    
     '   Exit the property if there is no file open
     If strAlias = "" Then
       Exit Property
     End If
        
     '   Get the position and return
     lngReturn = mciSendString("Status " & strAlias & " position", strPosition, 255, 0)
     intLength = InStr(strPosition, Chr(0))
     Position = Val(Left(strPosition, intLength - 1))
    End Property
    
      '
      '
    The value controls the movement of a vertical bar moving left to right across a picturebox which is representing the amount of playing time that has elapsed. So far the only API that has made it possible for me to get a precise timing on the playing time is the mciSendString. How can I use your code to get the same results?

    As I looked through the code I fail to see where/how it plays sounds and how it gets the sound to play from a buffer. Isn't there a commands like wavOutPlay, waveOutStop, etc
    Last edited by jmsrickland; May 19th, 2013 at 04:32 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  4. #4
    Fanatic Member namrekka's Avatar
    Join Date
    Feb 2005
    Location
    Netherlands
    Posts
    639

    Re: How to play sound from memory using mciSendString API

    Nop...no mciSendString. These are the underlaying API's.
    In this code it is not necessary to use 1 array. For example you can fill an array with 1 sec of samples (calculate this with the settings of the soundcard). In this code I didn't use a callback but a timer to check some flags. If the soundcard is ready then give the pointer of the next array...and so on. So every 1 sec you can fire an event and use an increment on the possition.

    Not sure why you want to stick with mciSendString.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to play sound from memory using mciSendString API

    So how do I record into an array and how do I play from an array using waveIn and waveOut.

    Let's say I open a wave file from disk and read it into a byte array. How do I get the samples out of the byte array and into your UDT called udtSamples?
    Last edited by jmsrickland; May 20th, 2013 at 06:38 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  6. #6
    Fanatic Member namrekka's Avatar
    Join Date
    Feb 2005
    Location
    Netherlands
    Posts
    639

    Re: How to play sound from memory using mciSendString API

    Hmm....that is an other question than: "How to play sound from memory using mciSendString API"

    I have to dive into my old prjects. I haven't been using VB6 for many years. I will have a look.

  7. #7
    Fanatic Member namrekka's Avatar
    Join Date
    Feb 2005
    Location
    Netherlands
    Posts
    639

    Re: How to play sound from memory using mciSendString API

    I wrote this once to check and adjust wav files. It checks if clipping appears. Also it adjust the "volume" of the wav files if needed.
    Code is as it is. It was 10 years agoo...lol
    Attached Files Attached Files

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to play sound from memory using mciSendString API

    Quote Originally Posted by namrekka View Post
    Hmm....that is an other question than: "How to play sound from memory using mciSendString API"
    Yes it is because I figured since your code didn't deal with mciSendString I might as well try and do it with your code using waveIn and waveOut. The only reason I asked how using mciSendString is because I already have an application that uses mciSendString to play but only from a file read-in from disk. Also, mciSendString returns the position where it is in the playing and I use that value to control a progress bar moving left to right as the sound plays. If I can get the same benefit from waveIn/waveOut then OK that's fine with me. Whatever works is what I'll use.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to play sound from memory using mciSendString API

    Quote Originally Posted by namrekka View Post
    I wrote this once to check and adjust wav files. It checks if clipping appears. Also it adjust the "volume" of the wav files if needed.
    Code is as it is. It was 10 years agoo...lol
    clsBrowseFolder.cls and modWaveFile.bas missing


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to play sound from memory using mciSendString API

    OK, I figured out how to play sound from an array using waveOut. Now all I need is to record into an array or a buffer of some sort.

    The other thing that I need and this is the most important is to get the position into the sound array during playback. Here is how it is done using mciSendString:

    lngReturn = mciSendString("Status " & strAlias & " position", strPosition, 255, 0)

    Now if I can get that value using waveOut and the ability to record into a buffer then I'll be all set


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  11. #11
    Fanatic Member namrekka's Avatar
    Join Date
    Feb 2005
    Location
    Netherlands
    Posts
    639

    Re: How to play sound from memory using mciSendString API

    Oops...I forgot to include some classes indeed.
    Attached Files Attached Files

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to play sound from memory using mciSendString API

    Finally got both play and record from array working. All I need now is how to get the position in the sound data during playing. Any ideas how to do that?


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to play sound from memory using mciSendString API

    Got it all to work


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width