2 Replies - 5443 Views - Last Post: 22 December 2008 - 06:43 PM Rate Topic: -----

#1 zeoez   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-December 08

Streaming raw audio data from Flash ActionScript to Visual Basic

Post icon  Posted 22 December 2008 - 05:45 PM

I have this crazy action script program that streams raw audio data to visual basic so visual basic uses DirectSound from DirectX to play the audio.

here's the ActionScript2.0 part(in the first frame):

// array to hold raw audio data
var pack:Array = new Array(44100); 

// the following fills 'pack' array with a simple Sinusoid wave at 441Hz, represents the audio data
var n:Number = Math.PI * 2 / 44100 * 441;
for(i=0; i<44100; i++) {
	pack[i] = Math.ceil(Math.sin(x * n) * 50) + 51; //51 is max. amplitude
}


// calls on VisualBasic once every 1000 miliseconds(1 second), passing 'pack' array with audio data
intervalID = setInterval(
		function () {
			fscommand("whatever",pack); // 'fscommand' declared by VisualBasic
		}, 1000);



On the visual basic side:
(VisualBasic 2005: Add a ShockwaveFlashObject to the form, name it "mc", and make sure to copy to the project folder)

Imports Microsoft.DirectX
Imports Microsoft.DirectX.DirectSound

Public Class Form1

	'Dim dDevice As New Device
	'Dim dxSound As SecondaryBuffer
	'Dim buff As DirectSound.Buffer
	'Dim desc As DirectSound.BufferDescription
	'Dim form As DirectSound.WaveFormat

	Dim sampRate% = 44100
	Dim buffRate% = 1	' buffer pend rate 
	Dim buffPos% = 0
	Dim channels% = 1 ' audio channels
	Dim buffTime = 1 ' 1 second
	Dim buffSize = sampRate * channels * buffTime

	Dim packetSize = (sampRate / buffRate) * channels
	Dim packet(packetSize) As Byte

	Dim datastream(packetSize) As String
	Dim samples(buffSize) As Byte
	Dim i% = 0, x% = 0

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'
		Call mc.LoadMovie(0, "D:\streamer\flashstream.swf")
		mc.Width = 192
		mc.Height = 106

		'dDevice.SetCooperativeLevel(Me, CooperativeLevel.Normal)
		' form.BitsPerSample = 8
		' form.SamplesPerSecond = sampRate
		' form.Channels = channels

		'form.FormatTag = WaveFormatTag.Pcm
		'form.BlockAlign = channels
		'form.AverageBytesPerSecond = sampRate * channels

		'desc = New DirectSound.BufferDescription(form)
		'desc.BufferBytes = buffSize
		'desc.Flags = BufferDescriptionFlags.StaticBuffer

		'dxSound = New SecondaryBuffer(desc, dDevice)

		sec = 0
		txt.Text = sec

	End Sub

	Private Sub mc_FSCommand(ByVal eventSender As System.Object, ByVal eventArgs As AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEvent) Handles mc.FSCommand

		'
		'If eventArgs.command = "cmd" Then
		'		If eventArgs.args = "play" Then
		'	   dxSound.Play(0, BufferPlayFlags.Looping)
		'		Else
		'	   dxSound.Stop()
		'	   ' -->flush buffer ???
		'	  End If

		'	   Else 'If packetSize = 11 Then
		'	  'txt.Text = eventArgs.args
		datastream = Split(eventArgs.args, ",")


		'	   'Array.Copy(datastream, samples, packetSize)
		'	   For i = buffPos To buffPos + packetSize - 1
		'		   samples(i) = datastream(x)
		'		 x += 1
		'	  Next
		'	  'txt.Text = buffPos & ":"
		'	  x = 0



		'	   buffPos += packetSize
		'	   If buffPos >= buffSize Then ' access and fill buffer only when samples are full
		'		   dxSound.Write(0, samples, LockFlag.EntireBuffer)
		'		   buffPos = 0
		'	   End If

		'		   'samples(i) = Format$(Hex$(datastream(i)), "00") 'CByte(datastream(i))
		'		   'samples = StrConv(eventArgs.args, VbStrConv.None)
		'		   txt.Text = StrConv(samples(0), VbStrConv.Uppercase) & ":" & StrConv(samples(100), VbStrConv.Uppercase) & ":" & StrConv(samples(200), VbStrConv.Uppercase)

		'		End If

		txt.Text = sec

		sec += 1
	End Sub










I have to put the data in a byte array in visual basic before I can stuff in a DirectSound buffer:

Dim packet() as Byte


Is This A Good Question/Topic? 0
  • +

Replies To: Streaming raw audio data from Flash ActionScript to Visual Basic

#2 zeoez   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-December 08

Re: Streaming raw audio data from Flash ActionScript to Visual Basic

Posted 22 December 2008 - 06:27 PM

I have this crazy action script program that streams raw audio data to visual basic so visual basic uses DirectSound from DirectX to play the audio. The problem I'm having is converting the data packets passed in from flash to a Byte array in VisualBasic. It's just too slow, see Visualbasic code below.

I need to figure out whether there is any other faster way to transfer the data from Flash to VB without any slow conversion of data types. Like direct memory access? (if we can identify the memory address of the array and pass that to VB, and have VB retrieve the data from that same address rather then transfer the data it self).

here's the ActionScript2.0 part which generates the raw data
(in the first frame. Export the movie as flashstream.swf):

// array to hold raw audio data
var packet:Array = new Array(44100); 

// the following fills 'packet' array with a simple Sinusoid wave at 441Hz, represents the audio data
var n:Number = Math.PI * 2 / 44100 * 441;
for(i=0; i<44100; i++) {
	packet[i] = Math.ceil(Math.sin(x * n) * 50) + 51; //51 is max. amplitude
}


// calls on VisualBasic once every 1000 miliseconds(1 second), passing 'packet' array containing audio data
intervalID = setInterval(
		function () {
			fscommand("whatever",packet); // fscommand' is declared in VisualBasic code
		}, 1000);





On the visual basic side:
(Add a ShockwaveFlashObject to the form, name it "mc", and make that you have the files 'AxInterop.ShockwaveFlashObjects.dll' and 'Interop.ShockwaveFlashObjects.dll' the project/Bin/Debug folder if using VB 2005)

-----------------------------------------------------------------
Imports Microsoft.DirectX
Imports Microsoft.DirectX.DirectSound

Public Class Form1

	Dim dDevice As New Device
	Dim dxSound As SecondaryBuffer
	Dim buff As DirectSound.Buffer
	Dim desc As DirectSound.BufferDescription
	Dim form As DirectSound.WaveFormat

	Dim sampRate% = 44100
	Dim buffRate% = 1	' buffer pend rate 
	Dim buffPos% = 0
	Dim channels% = 1 ' audio channels
	Dim buffTime = 1 ' 1 second
	Dim buffSize = sampRate * channels * buffTime

	Dim packetSize = (sampRate / buffRate) * channels
	Dim packet(packetSize) As Byte

	Dim datastream(packetSize) As String 'holds data passed from flash
	Dim samples(buffSize) As Byte 'holds the data to be written to DirectSound buffer
	Dim i% = 0, x% = 0

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

	   'attaches the flash movie to 'mc' object on the form
		Call mc.LoadMovie(0, "D:\streamer\flashstream.swf") ' use full path for VB 2005

		' define and start DirectSound properties and objects
		dDevice.SetCooperativeLevel(Me, CooperativeLevel.Normal)
		form.BitsPerSample = 8
		form.SamplesPerSecond = sampRate
		form.Channels = channels
		form.FormatTag = WaveFormatTag.Pcm
		form.BlockAlign = channels
		form.AverageBytesPerSecond = sampRate * channels
		desc = New DirectSound.BufferDescription(form)
		desc.BufferBytes = buffSize
		desc.Flags = BufferDescriptionFlags.StaticBuffer
		dxSound = New SecondaryBuffer(desc, dDevice)
	End Sub

	' function called from within the flash movie 'flashstream.swf'
	Private Sub mc_FSCommand(ByVal eventSender As System.Object, ByVal eventArgs As AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEvent) Handles mc.FSCommand

	   [color=#CC0000]'When the data packed is passed from Flash as an array, it is automatically imploded as one long string value separated by commas, so we need to split it back into an array:[/color]
	   datastream = Split(eventArgs.args, ",") [color=#CC0000]'<--  PROBLEM: this slows down the program to a crawl[/color]

	   [color=#CC0000]' DirectSound buffer only takes the data as a Byte Array type so we need to convert it[/color]
		' Array.Copy(datastream, samples, packetSize) <-- doesn't work, so we need to go though each member at a time:
		For i = buffPos To buffPos + packetSize - 1
			 samples(i) = datastream(x) [color=#CC0000]'<-- automaticaly converts String to Byte[/color]
		Next

		' populate DirectSound buffer at offset position 0
		 dxSound.Write(0, samples, LockFlag.EntireBuffer)
	End Sub

Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Streaming raw audio data from Flash ActionScript to Visual Basic

Posted 22 December 2008 - 06:43 PM

Please don't create duplicate topics, merged :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1