dcsimg
Results 1 to 6 of 6

Thread: mciSendString - Play Sound from ARRAY not PATH ?

  1. #1

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,495

    Question mciSendString - Play Sound from ARRAY not PATH ?

    Hello,

    I've been looking around and can't find a simple answer.

    is it possible to play a sound from an array buffer through mciSendString?

    I don't want to point to a path on the HD?


    And I also don't want to use use the sndPlaySound API. I know I can do it there.
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  2. #2

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,495

    Re: mciSendString - Play Sound from ARRAY not PATH ?

    50+ views and no one knows the answer
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,554

    Re: mciSendString - Play Sound from ARRAY not PATH ?

    Haven't thought about this for over a decade, so don't have a definitive answer for you. There are issues with some of the sound APIs and playing from memory & I don't recall if mciSendString has that limitation or not. Here's another thread that may answer your question or get you on a better path to a solution

    http://www.vbforums.com/showthread.p...-And-MID-Files!!
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,495

    Re: mciSendString - Play Sound from ARRAY not PATH ?

    Went through the thread and it mostly focuses on the sndPlaySound API to play a file from a resource, which is not quite what I want
    But I also found that the mciSendString can play from a resource file too, but still haven't found a way to play from an Array/Memory.

    I mainly need mciSendString to be able to play/handle wav files with loop points so that you can set it to play from startpos / end pos, rather than the whole audio.
    Perhaps any other APIs can achieve this?
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  5. #5
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,073

    Re: mciSendString - Play Sound from ARRAY not PATH ?

    To play from an array you need to load the wave file from HD into an array

    I don't know if this is loaded into an array or not but once you load it you can play it many times by referring to it's alias name meaning you can load more than one wave file and give each one a different alias

    Code:
    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
     (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As _
     Integer, ByVal hwndCallback As Integer) As Integer
    
    Dim myFile(0 To 10) As String
    
    Private Sub Form_Load()
     myFile(0) = Chr(34) & App.Path & "\your_wave_file.wav" & Chr(34)
    End Sub
    
    Private Sub Command1_Click() 'Play
     Call mciSendString("open " & myFile(0) & " type waveaudio alias Alias1", 0&, 0, 0)
    
     mciSendString "play Alias1", 0&, 0, 0
    End Sub
    
    Private Sub Command2_Click() 'Pause
     mciSendString "pause Alias1", vbNullString, 0, 0
    End Sub
    
    Private Sub Command3_Click() 'Resume
     mciSendString "resume Alias1", vbNullString, 0, 0
    End Sub
    
    Private Sub Command4_Click() 'Stop
     mciSendString "stop Alias1", vbNullString, 0, 0
    End Sub
    
    Private Sub Command5_Click() 'Close
     mciSendString "close Alias1", vbNullString, 0, 0
    End Sub
    I think if you want total control over the wave files like you suggested above you will probably need to use the waveIn/waveOut APIs
    Last edited by jmsrickland; Feb 24th, 2016 at 08:56 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
    New Member
    Join Date
    Mar 2016
    Location
    Providence, RI
    Posts
    13

    Re: mciSendString - Play Sound from ARRAY not PATH ?

    Hi I found this code in my archives from the good ol days. I hope it helps as it does generate a note in memory. I haven't figured out yet how to use a loop to paly multi notes but maybe you can come up with something.
    Option Explicit
    'Name length Constant
    Const MAXPNAMELEN = 32
    Dim hMidi As Long ' Holds Midi Port Handle to reference aspecific opened port

    'Midi Note format
    Const KeyOn = &H90 'Simulates Key Press
    Const KeyOff = &H80 'Simulates Key Release

    Private Type MidiMsg
    status As Byte 'Key On or Off
    Note As Byte 'Note - 60 is middle C each increment is a half step
    Volume As Byte 'Simulates how hard key was struck 0 - 127
    Data3 As Byte 'Not used in this example
    End Type

    'Holds in formation about device (Port)
    Private Type MIDIOUTCAPS
    wMid As Integer
    wPid As Integer
    vDriverVersion As Long
    szPname As String * MAXPNAMELEN
    wTechnology As Integer
    wVoices As Integer
    wNotes As Integer
    wChannelMask As Integer
    dwSupport As Long
    End Type

    'Callback routed to form - not used but necessary
    Private Const CALLBACK_WINDOW = &H10000
    Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
    'Send note to Midi Device (Port)
    Private Declare Function midiOutShortMsg Lib "winmm.dll" (ByVal hMidiOut As Long, ByVal dwMsg As Long) As Long
    'Close Port
    Private Declare Function midiOutClose Lib "winmm.dll" (ByVal hMidiOut As Long) As Long
    'Open a specific Midi Port
    Private Declare Function midiOutOpen Lib "winmm.dll" (lphMidiOut As Long, ByVal uDeviceID As Long, ByVal dwCallback As Long, ByVal dwInstance As Long, ByVal dwFlags As Long) As Long
    'Get how Many Ports are available
    Private Declare Function midiOutGetNumDevs Lib "winmm" () As Integer
    'Get info from specific port
    Private Declare Function midiOutGetDevCaps Lib "winmm.dll" Alias "midiOutGetDevCapsA" (ByVal uDeviceID As Long, lpCaps As MIDIOUTCAPS, ByVal uSize As Long) As Long
    'Used to convert Note Message to an unsigned Long Integer for API compatibility
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)

    Private Sub Combo1_Click()
    'Close any open ports
    midiOutClose hMidi
    'Open Port indicated in Combo Box
    midiOutOpen hMidi, Combo1.ListIndex, Me.hWnd, 0, CALLBACK_WINDOW
    End Sub
    Private Sub Command1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim msg As MidiMsg, ml As Long
    'set up note info
    msg.status = KeyOn
    msg.Volume = 64
    msg.Data3 = 0

    'choose note accroding to key pressed
    msg.Note = Choose(Index + 1, 60, 62, 64, 65, 67, 69, 71)
    CopyMemory ml, msg, 4 'copy to unsigned long integer
    midiOutShortMsg hMidi, ml 'send note

    'send next note
    msg.Note = Choose(Index + 1, 64, 65, 67, 69, 71, 72, 74)
    CopyMemory ml, msg, 4
    midiOutShortMsg hMidi, ml

    'send third note in triad
    msg.Note = Choose(Index + 1, 67, 69, 71, 72, 74, 76, 77)
    CopyMemory ml, msg, 4
    midiOutShortMsg hMidi, ml

    End Sub

    Private Sub Command1_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim msg As MidiMsg, ml As Long

    'if Right mouse button pressed, sustain note
    If Button = 2 Then Exit Sub
    'set up note off
    msg.status = KeyOff
    msg.Volume = 64
    msg.Data3 = 0

    'shut off first note
    msg.Note = Choose(Index + 1, 60, 62, 64, 65, 67, 69, 71)
    CopyMemory ml, msg, 4
    midiOutShortMsg hMidi, ml

    'shut off next note
    msg.Note = Choose(Index + 1, 64, 65, 67, 69, 71, 72, 74)
    CopyMemory ml, msg, 4
    midiOutShortMsg hMidi, ml

    'shut off last note in triad
    msg.Note = Choose(Index + 1, 67, 69, 71, 72, 74, 76, 77)
    CopyMemory ml, msg, 4
    midiOutShortMsg hMidi, ml

    End Sub

    Private Sub Form_Load()
    Dim i As Long, caps As MIDIOUTCAPS
    Show
    Me.ScaleMode = vbPixels
    Me.Caption = "Diatonic Chords,Mouse Button: Left-Staccato, Right-Legato"
    Combo1.Left = 0
    Combo1.Top = 0

    'Get number of devices and load names into combo box
    For i = 0 To midiOutGetNumDevs() - 1
    midiOutGetDevCaps i, caps, Len(caps)
    Combo1.AddItem caps.szPname, i
    Next
    Combo1.ListIndex = 0

    For i = 0 To 6

    Command1(0).Width = Me.ScaleWidth / 7
    If i > 0 Then Load Command1(i)
    With Command1(i)
    .Left = Me.ScaleWidth / 7 * i
    .Visible = True
    .Top = Me.ScaleHeight / 2 - .Height / 2
    .Caption = Choose(i + 1, "C", "Dm", "Em", "F", "G", "Am", "B")
    End With
    Next
    PlayNote 60
    End
    End Sub

    Private Sub Form_Unload(Cancel As Integer)
    'Close Port
    midiOutClose hMidi
    End Sub



    Public Sub Play(Pitch As Integer)
    msg.status = KeyOn
    msg.Volume = 127
    msg.Data3 = 0
    msg.Note = Pitch
    CopyMemory ml, msg, 4
    midiOutShortMsg hMidi, ml
    End Sub
    Public Sub StopPlay()
    msg.status = KeyOff
    msg.Data3 = 0
    msg.Note = 0
    CopyMemory ml, msg, 4
    midiOutShortMsg hMidi, ml
    End Sub

    Sub PlayNote(Freq As Double)
    Dim msg As MidiMsg, ml As Long
    msg.status = &H90
    msg.Note = Freq
    msg.Volume = 120
    msg.Data3 = 0
    CopyMemory ml, msg, 4
    midiOutShortMsg hMidi, ml
    End Sub

Tags for this Thread

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