[PD-cvs] pd/portaudio/pa_common pa_allocation.c,1.2,1.3 pa_allocation.h,1.2,1.3 pa_converters.c,1.2,1.3 pa_converters.h,1.2,1.3

Miller Puckette millerpuckette at users.sourceforge.net
Mon Sep 6 23:03:04 CEST 2004


Update of /cvsroot/pure-data/pd/portaudio/pa_common
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19253/portaudio/pa_common

Added Files:
	pa_allocation.c pa_allocation.h pa_converters.c 
	pa_converters.h 
Log Message:
More files I forgot to add



--- NEW FILE: pa_allocation.c ---
/*
 * $Id: pa_allocation.c,v 1.3 2004/09/06 21:03:01 millerpuckette Exp $
 * Portable Audio I/O Library allocation group implementation
 * memory allocation group for tracking allocation groups
 *
 * Based on the Open Source API proposed by Ross Bencina
 * Copyright (c) 1999-2002 Ross Bencina, Phil Burk
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * Any person wishing to distribute modifications to the Software is
 * requested to send the modifications to the original developer so that
 * they can be incorporated into the canonical version.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/** @file
 @brief Allocation Group implementation.
*/


#include "pa_allocation.h"
#include "pa_util.h"


/*
    Maintain 3 singly linked lists...
    linkBlocks: the buffers used to allocate the links
    spareLinks: links available for use in the allocations list
    allocations: the buffers currently allocated using PaUtil_ContextAllocateMemory()

    Link block size is doubled every time new links are allocated.
*/


#define PA_INITIAL_LINK_COUNT_    16

struct PaUtilAllocationGroupLink
{
    struct PaUtilAllocationGroupLink *next;
    void *buffer;
};

/*
    Allocate a block of links. The first link will have it's buffer member
    pointing to the block, and it's next member set to <nextBlock>. The remaining
    links will have NULL buffer members, and each link will point to
    the next link except the last, which will point to <nextSpare>
*/
static struct PaUtilAllocationGroupLink *AllocateLinks( long count,
        struct PaUtilAllocationGroupLink *nextBlock,
        struct PaUtilAllocationGroupLink *nextSpare )
{
    struct PaUtilAllocationGroupLink *result;
    int i;
    
    result = (struct PaUtilAllocationGroupLink *)PaUtil_AllocateMemory(
            sizeof(struct PaUtilAllocationGroupLink) * count );
    if( result )
    {
        /* the block link */
        result[0].buffer = result;
        result[0].next = nextBlock;

        /* the spare links */
        for( i=1; i<count; ++i )
        {
            result[i].buffer = 0;
            result[i].next = &result[i+1];
        }
        result[count-1].next = nextSpare;
    }
    
    return result;
}


PaUtilAllocationGroup* PaUtil_CreateAllocationGroup( void )
{
    PaUtilAllocationGroup* result = 0;
    struct PaUtilAllocationGroupLink *links;


    links = AllocateLinks( PA_INITIAL_LINK_COUNT_, 0, 0 );
    if( links != 0 )
    {
        result = (PaUtilAllocationGroup*)PaUtil_AllocateMemory( sizeof(PaUtilAllocationGroup) );
        if( result )
        {
            result->linkCount = PA_INITIAL_LINK_COUNT_;
            result->linkBlocks = &links[0];
            result->spareLinks = &links[1];
            result->allocations = 0;
        }
        else
        {
            PaUtil_FreeMemory( links );
        }
    }

    return result;
}


void PaUtil_DestroyAllocationGroup( PaUtilAllocationGroup* group )
{
    struct PaUtilAllocationGroupLink *current = group->linkBlocks;
    struct PaUtilAllocationGroupLink *next;

    while( current )
    {
        next = current->next;
        PaUtil_FreeMemory( current->buffer );
        current = next;
    }

    PaUtil_FreeMemory( group );
}


void* PaUtil_GroupAllocateMemory( PaUtilAllocationGroup* group, long size )
{
    struct PaUtilAllocationGroupLink *links, *link;
    void *result = 0;
    
    /* allocate more links if necessary */
    if( !group->spareLinks )
    {
        /* double the link count on each block allocation */
        links = AllocateLinks( group->linkCount, group->linkBlocks, group->spareLinks );
        if( links )
        {
            group->linkCount += group->linkCount;
            group->linkBlocks = &links[0];
            group->spareLinks = &links[1];
        }
    }

    if( group->spareLinks )
    {
        result = PaUtil_AllocateMemory( size );
        if( result )
        {
            link = group->spareLinks;
            group->spareLinks = link->next;

            link->buffer = result;
            link->next = group->allocations;

            group->allocations = link;
        }
    }

    return result;    
}


void PaUtil_GroupFreeMemory( PaUtilAllocationGroup* group, void *buffer )
{
    struct PaUtilAllocationGroupLink *current = group->allocations;
    struct PaUtilAllocationGroupLink *previous = 0;

    if( buffer == 0 )
        return;

    /* find the right link and remove it */
    while( current )
    {
        if( current->buffer == buffer )
        {
            previous->next = current->next;

            current->buffer = 0;
            current->next = group->spareLinks;
            group->spareLinks = current;
        }
        previous = current;
        current = current->next;
    }

    PaUtil_FreeMemory( buffer ); /* free the memory whether we found it in the list or not */
}


void PaUtil_FreeAllAllocations( PaUtilAllocationGroup* group )
{
    struct PaUtilAllocationGroupLink *current = group->allocations;
    struct PaUtilAllocationGroupLink *previous = 0;

    /* free all buffers in the allocations list */
    while( current )
    {
        PaUtil_FreeMemory( current->buffer );
        current->buffer = 0;

        previous = current;
        current = current->next;
    }

    /* link the former allocations list onto the front of the spareLinks list */
    if( previous )
    {
        previous->next = group->spareLinks;
        group->spareLinks = group->allocations;
        group->allocations = 0;
    }
}


--- NEW FILE: pa_converters.c ---
/*
 * $Id: pa_converters.c,v 1.3 2004/09/06 21:03:01 millerpuckette Exp $
 * Portable Audio I/O Library sample conversion mechanism
 *
 * Based on the Open Source API proposed by Ross Bencina
 * Copyright (c) 1999-2002 Phil Burk, Ross Bencina
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * Any person wishing to distribute modifications to the Software is
[...1886 lines suppressed...]
    {
        *dest = 0;

        dest += destinationStride;
    }
}

/* -------------------------------------------------------------------------- */

PaUtilZeroerTable paZeroers = {
    ZeroU8,  /* PaUtilZeroer *ZeroU8; */
    Zero8,  /* PaUtilZeroer *Zero8; */
    Zero16,  /* PaUtilZeroer *Zero16; */
    Zero24,  /* PaUtilZeroer *Zero24; */
    Zero32,  /* PaUtilZeroer *Zero32; */
};

/* -------------------------------------------------------------------------- */

#endif /* PA_NO_STANDARD_ZEROERS */

--- NEW FILE: pa_converters.h ---
#ifndef PA_CONVERTERS_H
#define PA_CONVERTERS_H
/*
 * $Id: pa_converters.h,v 1.3 2004/09/06 21:03:01 millerpuckette Exp $
 * Portable Audio I/O Library sample conversion mechanism
 *
 * Based on the Open Source API proposed by Ross Bencina
 * Copyright (c) 1999-2002 Phil Burk, Ross Bencina
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * Any person wishing to distribute modifications to the Software is
 * requested to send the modifications to the original developer so that
 * they can be incorporated into the canonical version.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/** @file
 @brief Conversion functions used to convert buffers of samples from one
 format to another.
*/


#include "portaudio.h"  /* for PaSampleFormat */

#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */


struct PaUtilTriangularDitherGenerator;


/** Choose an available sample format which is most appropriate for
 representing the requested format. If the requested format is not available
 higher quality formats are considered before lower quality formates.
 @param availableFormats A variable containing the logical OR of all available
 formats.
 @param format The desired format.
 @return The most appropriate available format for representing the requested
 format.
*/
PaSampleFormat PaUtil_SelectClosestAvailableFormat(
        PaSampleFormat availableFormats, PaSampleFormat format );


/* high level conversions functions for use by implementations */


/** The generic sample converter prototype. Sample converters convert count
    samples from sourceBuffer to destinationBuffer. The actual type of the data
    pointed to by these parameters varys for different converter functions.
    @param destinationBuffer A pointer to the first sample of the destination.
    @param destinationStride An offset between successive destination samples
    expressed in samples (not bytes.) It may be negative.
    @param sourceBuffer A pointer to the first sample of the source.
    @param sourceStride An offset between successive source samples
    expressed in samples (not bytes.) It may be negative.
    @param count The number of samples to convert.
    @param ditherState State information used to calculate dither. Converters
    that do not perform dithering will ignore this parameter, in which case
    NULL or invalid dither state may be passed.
*/
typedef void PaUtilConverter(
    void *destinationBuffer, signed int destinationStride,
    void *sourceBuffer, signed int sourceStride,
    unsigned int count, struct PaUtilTriangularDitherGenerator *ditherGenerator );


/** Find a sample converter function for the given source and destinations
    formats and flags (clip and dither.)
    @return
    A pointer to a PaUtilConverter which will perform the requested
    conversion, or NULL if the given format conversion is not supported.
    For conversions where clipping or dithering is not necessary, the
    clip and dither flags are ignored and a non-clipping or dithering
    version is returned.
    If the source and destination formats are the same, a function which
    copies data of the appropriate size will be returned.
*/
PaUtilConverter* PaUtil_SelectConverter( PaSampleFormat sourceFormat,
        PaSampleFormat destinationFormat, PaStreamFlags flags );


/** The generic buffer zeroer prototype. Buffer zeroers copy count zeros to
    destinationBuffer. The actual type of the data pointed to varys for
    different zeroer functions.
    @param destinationBuffer A pointer to the first sample of the destination.
    @param destinationStride An offset between successive destination samples
    expressed in samples (not bytes.) It may be negative.
    @param count The number of samples to zero.
*/
typedef void PaUtilZeroer(
    void *destinationBuffer, signed int destinationStride, unsigned int count );

    
/** Find a buffer zeroer function for the given destination format.
    @return
    A pointer to a PaUtilZeroer which will perform the requested
    zeroing.
*/
PaUtilZeroer* PaUtil_SelectZeroer( PaSampleFormat destinationFormat );

/*----------------------------------------------------------------------------*/
/* low level functions and data structures which may be used for
    substituting conversion functions */


/** The type used to store all sample conversion functions.
    @see paConverters;
*/
typedef struct{
    PaUtilConverter *Float32_To_Int32;
    PaUtilConverter *Float32_To_Int32_Dither;
    PaUtilConverter *Float32_To_Int32_Clip;
    PaUtilConverter *Float32_To_Int32_DitherClip;

    PaUtilConverter *Float32_To_Int24;
    PaUtilConverter *Float32_To_Int24_Dither;
    PaUtilConverter *Float32_To_Int24_Clip;
    PaUtilConverter *Float32_To_Int24_DitherClip;
    
    PaUtilConverter *Float32_To_Int16;
    PaUtilConverter *Float32_To_Int16_Dither;
    PaUtilConverter *Float32_To_Int16_Clip;
    PaUtilConverter *Float32_To_Int16_DitherClip;

    PaUtilConverter *Float32_To_Int8;
    PaUtilConverter *Float32_To_Int8_Dither;
    PaUtilConverter *Float32_To_Int8_Clip;
    PaUtilConverter *Float32_To_Int8_DitherClip;

    PaUtilConverter *Float32_To_UInt8;
    PaUtilConverter *Float32_To_UInt8_Dither;
    PaUtilConverter *Float32_To_UInt8_Clip;
    PaUtilConverter *Float32_To_UInt8_DitherClip;

    PaUtilConverter *Int32_To_Float32;
    PaUtilConverter *Int32_To_Int24;
    PaUtilConverter *Int32_To_Int24_Dither;
    PaUtilConverter *Int32_To_Int16;
    PaUtilConverter *Int32_To_Int16_Dither;
    PaUtilConverter *Int32_To_Int8;
    PaUtilConverter *Int32_To_Int8_Dither;
    PaUtilConverter *Int32_To_UInt8;
    PaUtilConverter *Int32_To_UInt8_Dither;

    PaUtilConverter *Int24_To_Float32;
    PaUtilConverter *Int24_To_Int32;
    PaUtilConverter *Int24_To_Int16;
    PaUtilConverter *Int24_To_Int16_Dither;
    PaUtilConverter *Int24_To_Int8;
    PaUtilConverter *Int24_To_Int8_Dither;
    PaUtilConverter *Int24_To_UInt8;
    PaUtilConverter *Int24_To_UInt8_Dither;

    PaUtilConverter *Int16_To_Float32;
    PaUtilConverter *Int16_To_Int32;
    PaUtilConverter *Int16_To_Int24;
    PaUtilConverter *Int16_To_Int8;
    PaUtilConverter *Int16_To_Int8_Dither;
    PaUtilConverter *Int16_To_UInt8;
    PaUtilConverter *Int16_To_UInt8_Dither;

    PaUtilConverter *Int8_To_Float32;
    PaUtilConverter *Int8_To_Int32;
    PaUtilConverter *Int8_To_Int24;
    PaUtilConverter *Int8_To_Int16;
    PaUtilConverter *Int8_To_UInt8;
    
    PaUtilConverter *UInt8_To_Float32;
    PaUtilConverter *UInt8_To_Int32;
    PaUtilConverter *UInt8_To_Int24;
    PaUtilConverter *UInt8_To_Int16;
    PaUtilConverter *UInt8_To_Int8;

    PaUtilConverter *Copy_8_To_8;       /* copy without any conversion */
    PaUtilConverter *Copy_16_To_16;     /* copy without any conversion */
    PaUtilConverter *Copy_24_To_24;     /* copy without any conversion */
    PaUtilConverter *Copy_32_To_32;     /* copy without any conversion */
} PaUtilConverterTable;


/** A table of pointers to all required converter functions.
    PaUtil_SelectConverter() uses this table to lookup the appropriate
    conversion functions. The fields of this structure are initialized
    with default conversion functions. Fields may be NULL, indicating that
    no conversion function is available. User code may substitue optimised
    conversion functions by assigning different function pointers to
    these fields.

    @note
    If the PA_NO_STANDARD_CONVERTERS preprocessor variable is defined,
    PortAudio's standard converters will not be compiled, and all fields
    of this structure will be initialized to NULL. In such cases, users
    should supply their own conversion functions if the require PortAudio
    to open a stream that requires sample conversion.

    @see PaUtilConverterTable, PaUtilConverter, PaUtil_SelectConverter
*/
extern PaUtilConverterTable paConverters;


/** The type used to store all buffer zeroing functions.
    @see paZeroers;
*/
typedef struct{
    PaUtilZeroer *ZeroU8; /* unsigned 8 bit, zero == 128 */
    PaUtilZeroer *Zero8;
    PaUtilZeroer *Zero16;
    PaUtilZeroer *Zero24;
    PaUtilZeroer *Zero32;
} PaUtilZeroerTable;


/** A table of pointers to all required zeroer functions.
    PaUtil_SelectZeroer() uses this table to lookup the appropriate
    conversion functions. The fields of this structure are initialized
    with default conversion functions. User code may substitue optimised
    conversion functions by assigning different function pointers to
    these fields.

    @note
    If the PA_NO_STANDARD_ZEROERS preprocessor variable is defined,
    PortAudio's standard zeroers will not be compiled, and all fields
    of this structure will be initialized to NULL. In such cases, users
    should supply their own zeroing functions for the sample sizes which
    they intend to use.

    @see PaUtilZeroerTable, PaUtilZeroer, PaUtil_SelectZeroer
*/
extern PaUtilZeroerTable paZeroers;

#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* PA_CONVERTERS_H */

--- NEW FILE: pa_allocation.h ---
#ifndef PA_ALLOCATION_H
#define PA_ALLOCATION_H
/*
 * $Id: pa_allocation.h,v 1.3 2004/09/06 21:03:01 millerpuckette Exp $
 * Portable Audio I/O Library allocation context header
 * memory allocation context for tracking allocation groups
 *
 * Based on the Open Source API proposed by Ross Bencina
 * Copyright (c) 1999-2002 Ross Bencina, Phil Burk
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * Any person wishing to distribute modifications to the Software is
 * requested to send the modifications to the original developer so that
 * they can be incorporated into the canonical version.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/** @file
 @brief Allocation Group prototypes. An Allocation Group makes it easy to
 allocate multiple blocks of memory and free them all simultanously.
 
 An allocation group is useful for keeping track of multiple blocks
 of memory which are allocated at the same time (such as during initialization)
 and need to be deallocated at the same time. The allocation group maintains
 a list of allocated blocks, and can deallocate them all simultaneously which
 can be usefull for cleaning up after a partially initialized object fails.

 The allocation group implementation is built on top of the lower
 level allocation functions defined in pa_util.h
*/


#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */


typedef struct
{
    long linkCount;
    struct PaUtilAllocationGroupLink *linkBlocks;
    struct PaUtilAllocationGroupLink *spareLinks;
    struct PaUtilAllocationGroupLink *allocations;
}PaUtilAllocationGroup;



/** Create an allocation group.
*/
PaUtilAllocationGroup* PaUtil_CreateAllocationGroup( void );

/** Destroy an allocation group, but not the memory allocated through the group.
*/
void PaUtil_DestroyAllocationGroup( PaUtilAllocationGroup* group );

/** Allocate a block of memory though an allocation group.
*/
void* PaUtil_GroupAllocateMemory( PaUtilAllocationGroup* group, long size );

/** Free a block of memory that was previously allocated though an allocation
 group. Calling this function is a relatively time consuming operation.
 Under normal circumstances clients should call PaUtil_FreeAllAllocations to
 free all allocated blocks simultaneously.
 @see PaUtil_FreeAllAllocations
*/
void PaUtil_GroupFreeMemory( PaUtilAllocationGroup* group, void *buffer );

/** Free all blocks of memory which have been allocated through the allocation
 group. This function doesn't destroy the group itself.
*/
void PaUtil_FreeAllAllocations( PaUtilAllocationGroup* group );


#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* PA_ALLOCATION_H */





More information about the Pd-cvs mailing list