It is currently Fri May 24, 2013 11:35 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Audio Synthesis
PostPosted: Mon Feb 25, 2013 6:40 pm 
Offline
Addict
Addict

Joined: Sun Aug 08, 2004 5:21 am
Posts: 1086
Location: Netherlands
The code below creates a simple audio stream for audio synthesis.
What it does is already possible using PortAudio but this is a native approach not requiring anything else.

Include file
Code:
; AudioStream.pbi

#AUDIOBUFFER_SIZE = 16384
#AUDIOBUFFER_SAMPLES = #AUDIOBUFFER_SIZE >> 3

Structure AudioStreamBasicDescription
  mSampleRate.d
  mFormatID.l
  mFormatFlags.l
  mBytesPerPacket.l
  mFramesPerPacket.l
  mBytesPerFrame.l
  mChannelsPerFrame.l
  mBitsPerChannel.l
  mReserved.l
EndStructure

Structure AudioQueueBuffer
  mAudioDataBytesCapacity.i
  mAudioData.i
  mAudioDataByteSize.i
  mUserData.i
  mPacketDescriptionCapacity.i
  mPacketDescriptions.i
  mPacketDescriptionCount.i
EndStructure

Structure AudioStreamStereoSample
  l.f
  r.f
EndStructure

Structure AudioStreamSamples
  StructureUnion
    s.d[0]
    ss.AudioStreamStereoSample[0]
  EndStructureUnion
EndStructure

ImportC "/System/Library/Frameworks/AudioToolbox.framework/AudioToolbox"
  AudioQueueAllocateBuffer(inAQ, inBufferByteSize, *outBuffer)
  AudioQueueDispose(inAQ, inImmediate = #True)
  AudioQueueEnqueueBuffer(inAQ, inBuffer, inNumPacketDescs = 0, *inPacketDescs = #Null)
  AudioQueueNewOutput(*inFormat, inCallbackProc, *inUserData, inCallbackRunLoop, inCallbackRunLoopMode, inFlags, *outAQ)
  AudioQueueSetParameter(inAQ, inParamID, inValue.f)
  AudioQueueStart(inAQ, *inStartTime = #Null)
  AudioQueueStop(inAQ, inImmediate = #True)
EndImport

Prototype AudioStream_Callback_Proto(*SampleData.Float)
Global AudioStream_Callback.AudioStream_Callback_Proto

Global *AudioStream_Buf1.AudioQueueBuffer, *AudioStream_Buf2.AudioQueueBuffer
Global AudioStream_Queue = 0

ProcedureC AudioStream_Callback_(*ptr, queue, *buf.AudioQueueBuffer)
  AudioStream_Callback(*buf\mAudioData)
  AudioQueueEnqueueBuffer(queue, *buf)
EndProcedure

Procedure AudioStream_Init(*Callback, Stereo = #False)
  AudioStream_Callback = *Callback
  Protected fmt.AudioStreamBasicDescription
  fmt\mSampleRate = 44100
  fmt\mFormatID = $6C70636D; kAudioFormatLinearPCM
  fmt\mFormatFlags = 1; kAudioFormatFlagIsFloat
  fmt\mFramesPerPacket = 1
  fmt\mBytesPerFrame = 8
  fmt\mBytesPerPacket = 8
  If Stereo
    fmt\mChannelsPerFrame = 2
    fmt\mBitsPerChannel = 32
  Else
    fmt\mChannelsPerFrame = 1
    fmt\mBitsPerChannel = 64
  EndIf
  If AudioQueueNewOutput(@fmt, @AudioStream_Callback_(), #Null, #Null, #Null, 0, @AudioStream_Queue) = 0
    AudioQueueAllocateBuffer(AudioStream_Queue, #AUDIOBUFFER_SIZE, @*AudioStream_Buf1)
    AudioQueueAllocateBuffer(AudioStream_Queue, #AUDIOBUFFER_SIZE, @*AudioStream_Buf2)
    *AudioStream_Buf1\mAudioDataByteSize = #AUDIOBUFFER_SIZE
    *AudioStream_Buf2\mAudioDataByteSize = #AUDIOBUFFER_SIZE
  EndIf
EndProcedure

Procedure AudioStream_SetVolume(volume.f)
  If AudioStream_Queue
    AudioQueueSetParameter(AudioStream_Queue, 1, volume); 1 = kAudioQueueParam_Volume
  EndIf
EndProcedure

Procedure AudioStream_Start()
  If AudioStream_Queue
    AudioStream_Callback_(#Null, AudioStream_Queue, *AudioStream_Buf1)
    AudioStream_Callback_(#Null, AudioStream_Queue, *AudioStream_Buf2)
    AudioQueueStart(AudioStream_Queue)
  EndIf
EndProcedure

Procedure AudioStream_Stop()
  If AudioStream_Queue
    AudioQueueStop(AudioStream_Queue)
  EndIf
EndProcedure

Procedure AudioStream_Dispose()
  If AudioStream_Queue
    AudioQueueDispose(AudioStream_Queue)
    AudioStream_Queue = 0 
  EndIf
EndProcedure

_________________
OS X 10.8.3, PB 5.11 x64


Last edited by wilbert on Tue Feb 26, 2013 11:12 am, edited 4 times in total.

Top
 Profile  
 
 Post subject: Re: Audio Synthesis
PostPosted: Mon Feb 25, 2013 6:42 pm 
Offline
Addict
Addict

Joined: Sun Aug 08, 2004 5:21 am
Posts: 1086
Location: Netherlands
Example code (mono)
Code:
EnableExplicit

XIncludeFile "AudioStream.pbi"

Global Phase.d

Procedure SampleCallback(*out.AudioStreamSamples)
  Protected i = 0
  While i < #AUDIOBUFFER_SAMPLES
    *out\s[i] = Sin(Phase) / 6
    Phase + 0.06
    i + 1
  Wend
EndProcedure

AudioStream_Init(@SampleCallback())
AudioStream_SetVolume(0.7)
AudioStream_Start()

MessageRequester("Audio Synthesis Demo", "Playing audio")

AudioStream_Dispose()


Example code (stereo)
Code:
EnableExplicit

XIncludeFile "AudioStream.pbi"

Global Phase.d

Procedure SampleCallback(*out.AudioStreamSamples)
  Protected i = 0
  While i < #AUDIOBUFFER_SAMPLES
    *out\ss[i]\l = Sin(Phase) / 6
    *out\ss[i]\r = Sin(Phase * 2) / 6
    Phase + 0.06
    i + 1
  Wend
EndProcedure

AudioStream_Init(@SampleCallback(), #True)
AudioStream_SetVolume(0.7)
AudioStream_Start()

MessageRequester("Audio Synthesis Demo", "Playing audio")

AudioStream_Dispose()

_________________
OS X 10.8.3, PB 5.11 x64


Last edited by wilbert on Tue Feb 26, 2013 11:13 am, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: Audio Synthesis
PostPosted: Mon Feb 25, 2013 8:39 pm 
Offline
Addict
Addict
User avatar

Joined: Mon Aug 04, 2008 10:56 pm
Posts: 849
Location: Seattle, USA
Thanks wilbert!

It works fine. Now just to figure out what to do with it... :)

_________________
MacBook Pro/Retina, OSX 10.8.3 Mountain Lion, PB-5.11x64


Top
 Profile  
 
 Post subject: Re: Audio Synthesis
PostPosted: Tue Feb 26, 2013 9:21 am 
Offline
Addict
Addict
User avatar

Joined: Sun Apr 27, 2003 8:12 am
Posts: 1620
Location: USA
Jiminy Cricket!!!

What ever you do, don't do "*output + 2" with headphones on.

Nice work wilbert! But I'll wait till day, when I don't need headphones on to fiddle around with it. ;)

_________________
AMD 64 4000+ / 1GB PC2700 / WIN XP Home SP3 / Nvidia GT220 x16 512MB / M-Audio Revolution 5.1
Macbook Air 11.6" - 2010 / OS X 10.8

http://www.posemotion.com
http://www.flashpulse.com


Top
 Profile  
 
 Post subject: Re: Audio Synthesis
PostPosted: Tue Feb 26, 2013 11:09 am 
Offline
Addict
Addict

Joined: Sun Aug 08, 2004 5:21 am
Posts: 1086
Location: Netherlands
J. Baker wrote:
What ever you do, don't do "*output + 2" with headphones on.

I made a similar mistake with speakers on; wasn't nice :shock:

Anyway, I changed the include file and example above.
I changed it so mono is 1 x 64-bit float and stereo is 2 x 32-bit float.
This way the number of samples is the same for both and with a structure union it is easier to access the samples (no need to increase a memory pointer).

@William,
Something like this would be nice http://tonematrix.audiotool.com
It's created by someone else using Flash but I suppose it should be possible to create something similar using the canvas gadget.

_________________
OS X 10.8.3, PB 5.11 x64


Top
 Profile  
 
 Post subject: Re: Audio Synthesis
PostPosted: Tue Feb 26, 2013 7:02 pm 
Offline
Addict
Addict
User avatar

Joined: Mon Aug 04, 2008 10:56 pm
Posts: 849
Location: Seattle, USA
wilbert,

tonematrix is a lot of fun and the site is a real time-waster :) I agree it looks like a canvas gadget waiting to happen.

I was listening to http://www.audiotool.com/track/yesterday-pa8gus/ ...enviro-synthesis (just made that up)

_________________
MacBook Pro/Retina, OSX 10.8.3 Mountain Lion, PB-5.11x64


Top
 Profile  
 
 Post subject: Re: Audio Synthesis
PostPosted: Tue Feb 26, 2013 8:35 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Apr 27, 2003 8:12 am
Posts: 1620
Location: USA
wilbert wrote:
J. Baker wrote:
What ever you do, don't do "*output + 2" with headphones on.

I made a similar mistake with speakers on; wasn't nice :shock:

Anyway, I changed the include file and example above.
I changed it so mono is 1 x 64-bit float and stereo is 2 x 32-bit float.
This way the number of samples is the same for both and with a structure union it is easier to access the samples (no need to increase a memory pointer).

Cool. ;)

_________________
AMD 64 4000+ / 1GB PC2700 / WIN XP Home SP3 / Nvidia GT220 x16 512MB / M-Audio Revolution 5.1
Macbook Air 11.6" - 2010 / OS X 10.8

http://www.posemotion.com
http://www.flashpulse.com


Top
 Profile  
 
 Post subject: Re: Audio Synthesis
PostPosted: Tue Feb 26, 2013 9:29 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Apr 27, 2003 8:12 am
Posts: 1620
Location: USA
Hello? Who is it? :D

Code:
EnableExplicit

XIncludeFile "AudioStream.pbi"

Global Phase.d

Procedure SampleCallback(*out.AudioStreamSamples)
  Protected i = 0
  While i < #AUDIOBUFFER_SAMPLES
    *out\s[i] = Sin(Phase) / 3
     If i / 500
       Phase + 0.35
     Else
       Phase + 0.25
     EndIf
    i + 1
  Wend
EndProcedure

Define Ring = 0

AudioStream_Init(@SampleCallback())
AudioStream_SetVolume(0.7)

Repeat
 
 AudioStream_Start()

  Delay(1000)

   AudioStream_Stop()

  Delay(100)

 Ring + 1

Until Ring = 2

AudioStream_Dispose()

_________________
AMD 64 4000+ / 1GB PC2700 / WIN XP Home SP3 / Nvidia GT220 x16 512MB / M-Audio Revolution 5.1
Macbook Air 11.6" - 2010 / OS X 10.8

http://www.posemotion.com
http://www.flashpulse.com


Top
 Profile  
 
 Post subject: Re: Audio Synthesis
PostPosted: Tue Feb 26, 2013 10:00 pm 
Offline
Addict
Addict

Joined: Sun Aug 08, 2004 5:21 am
Posts: 1086
Location: Netherlands
@Joe, was that your phone ringing ? :wink:

@William, you are right, it is fun that tone matrix. Don't know if it would be hard to recreate using PB.

_________________
OS X 10.8.3, PB 5.11 x64


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye