Sign in to follow this  
lkd85

[.net] Audiere in VB.NET

Recommended Posts

Hey ya'll, I've run into a very odd problem when trying to port the Audiere headers to VB (or .NET in general). Since it uses a interface system It's been easy to use in any language that supports Interfaces (Purebasic and Delphi). I made a successful port to D since it uses interfaces and I thought it would work with .NET since it supports COM. I've tried marshaling the interfaces to IUnknown, tried to use the Marshal.GetObjectForIUnknown method but it still fails. I have my D header port and my VB header port attached. Can someone give me any pointers on how to get this to work? Thanks. D Header
// /**
//  * Audiere Sound System
//  * Version 1.9.3
//  * (c) 2001-2003 Chad Austin
//  *
//  * This API uses principles explained at
//  * http://aegisknight.org/cppinterface.html
//  *
//  * This code licensed under the terms of the LGPL.  See doc/license.txt.

//Ported to D by LKD/2005
//Needs Watcom Linker to link library!

//Based on Purebasic port by WolfgangS@gmx.de

module audiere;
private import std.c.windows.com;

extern (Windows)
{
interface RefCounted
{
   void ref();                // int
   void unref();              // int
}

interface File: RefCounted, IUnknown
{
  int read(int pbuffer, int size);      // int
  bool seek(int position, int SeekMode);  // bool
  int tell();                       // int
}

interface SampleBuffer: RefCounted, IUnknown
{
  void getFormat(int channel_count,int sample_rate,int lsample_format);  // void
  int getLength();                                               // int
  int getSamples();                                              // int
  SampleSource openStream();                                              // pSampleSource
}

interface SampleSource: RefCounted, IUnknown
{
  void getFormat(int v,int v);      // void
  int Read(int v, int v);          // int
  void reset();                       // void
  bool isSeekable();                  // bool
  int getLength();                   // int
  void setPosition(int v);           // void
  int getPosition();                 // int
  bool getRepeat();                   // bool
  void setRepeat(int v);             // void
}

interface LoopPointSource: SampleSource, IUnknown
{
  void addLoopPoint(int location,int target, int loopCount);             // void
  void removeLoopPoint(int index);                                  // void
  int getLoopPointCount();                                       // int
  bool getLoopPoint(int index,int plocation,int ptarget,int ploopCount);  // bool
}

interface OutputStream: RefCounted, IUnknown
{
  bool play();
  bool stop();                 // bool
  bool isPlaying();            // bool
  void reset();                // void
  void setRepeat(int v);      // void
  bool getRepeat();            // bool
  void setVolume(float v);     // void
  float getVolume();            // float
  void setPan(float v);        // void
  float getPan();               // float
  void setPitchShift(float v); // void
  float getPitchShift();        // float
  bool isSeekable();           // bool
  int getLength();            // int
  void setPosition(int v);     // void
  int getPosition();          // int
}

interface AudioDevice: RefCounted, IUnknown
{
  void update();                               // void
  OutputStream openStream(SampleSource pSampleSource);            // pAudOutputStream
  OutputStream openBuffer(void* pSamples,int frame_count,int channel_count,int sample_rate,int sample_format);// pAudOutputStream
  char* getName();                              // char.l
}

interface SoundEffect: RefCounted, IUnknown
{
  void play();                          // void
  void stop();                          // void
  void setVolume(float volume);             // void
  float getVolume();                     // float
  void setPan(float pan);                   // void
  float getPan();                        // float
  void setPitchShift(float shift);          // void
  float getPitchShift();                 // float
}
}//End of extern(Windows)

enum SampleFormat
{
  SF_U8,      // unsigned 8-bit integer [0,255]
  SF_S16     // signed 16-bit integer in host endianness [-32768,32767]
}

enum FileFormat
{
  FF_AUTODETECT,
  FF_WAV,
  FF_OGG,
  FF_FLAC,
  FF_MP3,
  FF_MOD,
  FF_AIFF
}

enum SoundEffectType
{
  SINGLE,
  MULTIPLE
}

enum SeekMode
{
  begin,
  current,
  end
}


extern(Windows)
{
char* AdrGetVersion();
alias AdrGetVersion GetVersion;
    /**
     * Returns a formatted string that lists the file formats that Audiere
     * supports.  This function is DLL-safe.
     *
     * It is formatted in the following way:
     *
     * description1:ext1,ext2,ext3;description2:ext1,ext2,ext3
     */
char* AdrGetSupportedFileFormats();
alias AdrGetSupportedFileFormats GetSupportedFileFormats;

    /**
     * Returns a formatted string that lists the audio devices Audiere
     * supports.  This function is DLL-safe.
     *
     * It is formatted in the following way:
     *
     * name1:description1;name2:description2;...
     */
char* AdrGetSupportedAudioDevices();
alias AdrGetSupportedAudioDevices GetSupportedAudioDevices;

int AdrGetSampleSize(SampleFormat format);
alias AdrGetSampleSize GetSampleSize;

AudioDevice AdrOpenDevice(char* name = null, char* parameters = null);
alias AdrOpenDevice OpenDevice;

SampleSource AdrOpenSampleSource(char* filename, FileFormat file_format);
alias AdrOpenSampleSource OpenSampleSource;

SampleSource AdrOpenSampleSourceFromFile(File file, FileFormat file_format);
alias AdrOpenSampleSourceFromFile OpenSampleSourceFromFile;

SampleSource AdrCreateTone(double frequency);
alias AdrCreateTone CreateTone;

SampleSource AdrCreateSquareWave(double frequency);
alias AdrCreateSquareWave CreateSquareWave;

SampleSource AdrCreateWhiteNoise();
alias AdrCreateWhiteNoise CreateWhiteNoise;

SampleSource AdrCreatePinkNoise();
alias AdrCreatePinkNoise CreatePinkNoise;

LoopPointSource AdrCreateLoopPointSource(SampleSource source);
alias AdrCreateLoopPointSource CreateLoopPointSource;

OutputStream AdrOpenSound(AudioDevice device, SampleSource source, bool streaming);
alias AdrOpenSound OpenSound;

SampleBuffer AdrCreateSampleBuffer(void* samples, int frame_count, int channel_count,
      int sample_rate,
      SampleFormat sample_format);
alias AdrCreateSampleBuffer CreateSampleBuffer;

SampleBuffer AdrCreateSampleBufferFromSource(SampleSource source);
alias AdrCreateSampleBufferFromSource CreateSampleBufferFromSource;

SoundEffect AdrOpenSoundEffect(AudioDevice device, SampleSource source, SoundEffectType type);
alias AdrOpenSoundEffect OpenSoundEffect;

File AdrOpenFile(char* name, bool writeable);
alias AdrOpenFile OpenFile;

File AdrCreateMemoryFile(void* buffer, int size);
alias AdrCreateMemoryFile CreateMemoryFile;
}//End extern(Windows)

VB Header
Imports System.Runtime.InteropServices

Module Audiere

    ' <ComVisible(True), ComImport()> _
    Interface RefCounted
        Sub ref()                ' int
        Sub unref()              ' int
    End Interface

    '<InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
    Interface File : Inherits RefCounted
        Function read(ByVal pbuffer As Integer, ByVal size As Integer) As Integer      ' int
        Function seek(ByVal position As Integer, ByVal SeekMode As Integer) As Boolean  ' bool
        Function tell() As Integer                      ' int
    End Interface

    '<InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
    Interface SampleBuffer : Inherits RefCounted
        Sub getFormat(ByVal channel_count As Integer, ByVal sample_rate As Integer, ByVal lsample_format As Integer)  ' Sub
        Function getLength() As Integer                                                ' int
        Function getSamples() As Integer                                              ' int
        Function openStream() As SampleSource                                         ' pSampleSource
    End Interface

    '<InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
    Interface SampleSource : Inherits RefCounted
        Sub getFormat(ByVal v As Integer, ByVal v2 As Integer)      ' Sub
        Function Read(ByVal v As Integer, ByVal v2 As Integer) As Integer          ' int
        Sub reset()                       ' Sub
        Function isSeekable() As Boolean                  ' bool
        Function getLength() As Integer                   ' int
        Sub setPosition(ByVal v As Integer)           ' Sub
        Function getPosition() As Integer                  ' int
        Function getRepeat() As Boolean                   ' bool
        Sub setRepeat(ByVal v As Integer)             ' Sub
    End Interface

    '<InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
    Interface LoopPointSource : Inherits SampleSource
        Sub addLoopPoint(ByVal location As Integer, ByVal target As Integer, ByVal loopCount As Integer)             ' Sub
        Sub removeLoopPoint(ByVal index As Integer)                                  ' Sub
        Function getLoopPointCount() As Integer                                        ' int
        Function getLoopPoint(ByVal index As Integer, ByVal plocation As Integer, ByVal ptarget As Integer, ByVal ploopCount As Integer) As Boolean  ' bool
    End Interface

    '<InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
    Interface OutputStream : Inherits RefCounted
        Function play() As Boolean
        Function _stop() As Boolean                  ' bool
        Function isPlaying() As Boolean            ' bool
        Sub reset()                ' Sub
        Sub setRepeat(ByVal v As Boolean)      ' Sub
        Function getRepeat() As Boolean             ' bool
        Sub setVolume(ByVal v As Single)     ' Sub
        Function getVolume() As Single             ' float
        Sub setPan(ByVal v As Single)        ' Sub
        Function getPan() As Single               ' float
        Sub setPitchShift(ByVal v As Single) ' Sub
        Function getPitchShift() As Single         ' float
        Function isSeekable() As Boolean            ' bool
        Function getLength() As Integer             ' int
        Sub setPosition(ByVal v As Integer)     ' Sub
        Function getPosition() As Integer           ' int
    End Interface

    '<InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
    Interface AudioDevice : Inherits RefCounted
        Sub update()                           ' Sub
        Function openStream(ByRef pSampleSource As IntPtr) As IntPtr       ' pAudOutputStream
        Function openBuffer(ByRef pSamples As IntPtr, ByVal frame_count As Integer, ByVal channel_count As Integer, ByVal sample_rate As Integer, ByVal sample_format As Integer) As IntPtr  ' pAudOutputStream
        Function getName() As String                              ' char.l
    End Interface

    '<InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
    Interface SoundEffect : Inherits RefCounted
        Sub play()                          ' Sub
        Sub _stop()                          ' Sub
        Sub setVolume(ByVal volume As Single)             ' Sub
        Function getVolume() As Single                     ' float
        Sub setPan(ByVal pan As Single)                   ' Sub
        Function getPan() As Single                        ' float
        Sub setPitchShift(ByVal shift As Single)          ' Sub
        Function getPitchShift() As Single                 ' float
    End Interface


    Enum SampleFormat
        SF_U8      ' unsigned 8-bit integer [0,255]
        SF_S16     ' signed 16-bit integer in host end Interface ianness [-32768,32767]
    End Enum

    Enum FileFormat
        FF_AUTODETECT
        FF_WAV
        FF_OGG
        FF_FLAC
        FF_MP3
        FF_MOD
        FF_AIFF
    End Enum

    Enum SoundEffectType

        SE_SINGLE
        SE_MULTIPLE
    End Enum

    Enum SeekMode

        SM_Begin
        SM_Current
        SM_end
    End Enum

    'Functions
    'char* AdrGetVersion()

    'char* AdrGetSupportedFileFormats()

    'char* AdrGetSupportedAudioDevices()

    'int AdrGetSampleSize(SampleFormat format)

    'AudioDevice AdrOpenDevice(char* name = null, char* parameters = null)
    Declare Function OpenDevice Lib "audiere" Alias "_AdrOpenDevice@8" (Optional ByVal name As String = "", Optional ByVal parameters As String = "") As <MarshalAs(UnmanagedType.Interface)> AudioDevice

    'SampleSource AdrOpenSampleSource(char* filename, FileFormat file_format)

    'SampleSource AdrOpenSampleSourceFromFile(File file, FileFormat file_format)

    'SampleSource AdrCreateTone(double frequency)

    'SampleSource AdrCreateSquareWave(double frequency)

    'SampleSource AdrCreateWhiteNoise()

    'SampleSource AdrCreatePinkNoise()

    'LoopPointSource AdrCreateLoopPointSource(SampleSource source)

    'OutputStream AdrOpenSound(AudioDevice device, SampleSource source, bool streaming)

    'SampleBuffer AdrCreateSampleBuffer(Sub* samples, int frame_count, int channel_count, int sample_rate, SampleFormat sample_format)

    'SampleBuffer AdrCreateSampleBufferFromSource(SampleSource source)

    'SoundEffect AdrOpenSoundEffect(AudioDevice device, SampleSource source, SoundEffectType type)

    'File AdrOpenFile(char* name, bool writeable)

    'File AdrCreateMemoryFile(Sub* buffer, int size)
End Module


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this