Sending small segments of bytes to the waveOut API-VBForums
Results 1 to 3 of 3

Thread: Sending small segments of bytes to the waveOut API

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    9,149

    Sending small segments of bytes to the waveOut API

    I have an array full of sound data. Actually, it's 612000 bytes of sound data for this particular file that I loaded into a byte array

    How can I play small segments of this wave file at a time?

    I know I can send the entire 612000 bytes to the waveOutwrite API and it will play the entire sound file. No problem there. But let's say I want to only send a few at a time, like 3000 bytes or whatever at a time. So, how do I make a loop that pulls 3000 bytes out of the 612000 byte array, sends those bytes to the play API and continue on in the loop until I have sent all 612000 bytes?


    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
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Sending small segments of bytes to the waveOut API

    Here's something you could play with
    Code:
    Private Const SOUND_CHUNK As Long = 3000    ' Number of bytes to send
    
    Private Sub SendSoundChunks(bytSound() As Byte)
    Dim lngFullBuff As Long
    Dim lngPartBuff As Long
    Dim lngStartByte As Long
    Dim lngEndByte As Long
    Dim lngCount As Long
    Dim lngI As Long
    Dim lngJ As Long
    Dim lngK As Long
    Dim bytToSend() As Byte
    lngCount = UBound(bytSound) + 1
    lngFullBuff = lngCount \ SOUND_CHUNK        'Number of full 'chunks'
    lngPartBuff = lngCount Mod SOUND_CHUNK      'Remaining bytes
    lngStartByte = 0                            'First element to copy
    lngEndByte = lngStartByte + SOUND_CHUNK - 1 'Last element to copy
    ReDim bytToSend(SOUND_CHUNK - 1)            'Size the 'buffer' array
    '
    ' Send lngFullBuff number of 'chunks'
    ' by copying data from the original array to a 'buffer' array
    '
    For lngI = 1 To lngFullBuff
        lngK = 0
        For lngJ = lngStartByte To lngEndByte
            bytToSend(lngK) = bytSound(lngJ)
            lngK = lngK + 1
        Next lngJ
        '
        'here you'd send bytToSend to waveout
        '
        lngStartByte = lngEndByte + 1
        lngEndByte = lngStartByte + SOUND_CHUNK - 1
    Next lngI
    '
    'Now send the remaining bytes
    '(if there are any)
    '
    If lngPartBuff > 0 Then
        ReDim bytToSend(lngPartBuff - 1)
        lngEndByte = UBound(bytSound)
        lngK = 0
        For lngI = lngStartByte To lngEndByte
            bytToSend(lngK) = bytSound(lngI)
            lngK = lngK + 1
        Next lngI
        '
        'here you'd send bytToSend to waveout
        '
    End If
    End Sub
    Not the most efficient but perhaps the technique will give you some ideas.

    I suspect that lngK could be derived from lngI ,lngJ and SOUND_CHUNK and CopyMemory may be quicker than copying the arrray elements in a loop.

    The principle used is to calculate the number of full 'buffers' (lngFullBuff) of the 'chunk' size required (SOUND_CHUNK) and any remaining bytes (lngPartBuff), send lngFullBuff of 'chunks' and then send lngPartBuff bytes.
    Last edited by Doogle; Jun 28th, 2013 at 12:18 AM.

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    11,412

    Re: Sending small segments of bytes to the waveOut API

    Looks easy enough. Am I missing something?

    Code:
    Private Sub PretendAPI(ByVal Pointer As Long, ByVal Bytes As Long)
        Log "@ " & CStr(Pointer) & " for " & CStr(Bytes) & " bytes"
    End Sub
    
    Private Sub ChunkBuff(ByRef Buffer() As Byte)
        Const CHUNK As Long = 3000
        Dim ChunkStart As Long
        Dim Bytes As Long
        Dim NewBytes As Long
    
        Log "Buffer length is " & CStr(UBound(Buffer) + 1) & " bytes"
        Log
    
        Bytes = CHUNK
        For ChunkStart = 0 To UBound(Buffer) Step CHUNK
            NewBytes = UBound(Buffer) - ChunkStart + 1
            If NewBytes < CHUNK Then Bytes = NewBytes
            PretendAPI VarPtr(Buffer(ChunkStart)), Bytes
        Next
    End Sub
    Name:  sshot1.png
Views: 59
Size:  12.1 KB
    Attached Files Attached Files

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.