openalbridge/common.h
author koda
Thu, 15 Oct 2009 15:54:48 +0000
changeset 2454 c8b1fb10003c
parent 2445 5033848d3afa
child 2458 14296bd366d2
permissions -rw-r--r--
fix a potential bug when playing two sounds at the same time (or very near)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2445
5033848d3afa committing some missing files
koda
parents:
diff changeset
     1
/*
5033848d3afa committing some missing files
koda
parents:
diff changeset
     2
 * OpenAL Bridge - a simple portable library for OpenAL interface
5033848d3afa committing some missing files
koda
parents:
diff changeset
     3
 * Copyright (c) 2009 Vittorio Giovara <vittorio.giovara@gmail.com>,
5033848d3afa committing some missing files
koda
parents:
diff changeset
     4
 *                    Mario Liebisch <mario.liebisch+hw@googlemail.com>
5033848d3afa committing some missing files
koda
parents:
diff changeset
     5
 *
5033848d3afa committing some missing files
koda
parents:
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
5033848d3afa committing some missing files
koda
parents:
diff changeset
     7
 * it under the terms of the GNU Lesser General Public License as published by
5033848d3afa committing some missing files
koda
parents:
diff changeset
     8
 * the Free Software Foundation; version 2 of the License
5033848d3afa committing some missing files
koda
parents:
diff changeset
     9
 *
5033848d3afa committing some missing files
koda
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
5033848d3afa committing some missing files
koda
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5033848d3afa committing some missing files
koda
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5033848d3afa committing some missing files
koda
parents:
diff changeset
    13
 * GNU Lesser General Public License for more details.
5033848d3afa committing some missing files
koda
parents:
diff changeset
    14
 *
5033848d3afa committing some missing files
koda
parents:
diff changeset
    15
 * You should have received a copy of the GNU Lesser General Public License
5033848d3afa committing some missing files
koda
parents:
diff changeset
    16
 * along with this program; if not, write to the Free Software
5033848d3afa committing some missing files
koda
parents:
diff changeset
    17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
5033848d3afa committing some missing files
koda
parents:
diff changeset
    18
 */
5033848d3afa committing some missing files
koda
parents:
diff changeset
    19
2454
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
    20
2445
5033848d3afa committing some missing files
koda
parents:
diff changeset
    21
5033848d3afa committing some missing files
koda
parents:
diff changeset
    22
#include <stdio.h>
5033848d3afa committing some missing files
koda
parents:
diff changeset
    23
#include <stdlib.h>
5033848d3afa committing some missing files
koda
parents:
diff changeset
    24
#include <stdint.h>
5033848d3afa committing some missing files
koda
parents:
diff changeset
    25
#include <string.h>
5033848d3afa committing some missing files
koda
parents:
diff changeset
    26
#include <stdarg.h>
5033848d3afa committing some missing files
koda
parents:
diff changeset
    27
#include <errno.h>
5033848d3afa committing some missing files
koda
parents:
diff changeset
    28
#include "al.h"
5033848d3afa committing some missing files
koda
parents:
diff changeset
    29
#include "errlib.h"
5033848d3afa committing some missing files
koda
parents:
diff changeset
    30
5033848d3afa committing some missing files
koda
parents:
diff changeset
    31
#ifndef _WIN32
5033848d3afa committing some missing files
koda
parents:
diff changeset
    32
#include <pthread.h>
5033848d3afa committing some missing files
koda
parents:
diff changeset
    33
#include <syslog.h>
5033848d3afa committing some missing files
koda
parents:
diff changeset
    34
#else
5033848d3afa committing some missing files
koda
parents:
diff changeset
    35
#include <process.h>
5033848d3afa committing some missing files
koda
parents:
diff changeset
    36
#define syslog(x,y) fprintf(stderr,y)
5033848d3afa committing some missing files
koda
parents:
diff changeset
    37
#define LOG_INFO 6
5033848d3afa committing some missing files
koda
parents:
diff changeset
    38
#define LOG_ERR 3
5033848d3afa committing some missing files
koda
parents:
diff changeset
    39
#endif
5033848d3afa committing some missing files
koda
parents:
diff changeset
    40
2454
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
    41
#ifndef COMMON_H
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
    42
#define COMMON_H
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
    43
2445
5033848d3afa committing some missing files
koda
parents:
diff changeset
    44
/* magics */
5033848d3afa committing some missing files
koda
parents:
diff changeset
    45
#define OGG_FILE_FORMAT 0x4F676753
5033848d3afa committing some missing files
koda
parents:
diff changeset
    46
#define WAV_FILE_FORMAT 0x52494646
5033848d3afa committing some missing files
koda
parents:
diff changeset
    47
#define WAV_HEADER_SUBCHUNK2ID 0x64617461
5033848d3afa committing some missing files
koda
parents:
diff changeset
    48
5033848d3afa committing some missing files
koda
parents:
diff changeset
    49
#define MAX_SOUNDS 1024
5033848d3afa committing some missing files
koda
parents:
diff changeset
    50
#define MAX_SOURCES 16
5033848d3afa committing some missing files
koda
parents:
diff changeset
    51
5033848d3afa committing some missing files
koda
parents:
diff changeset
    52
/* check compiler requirements */    /*FIXME*/
5033848d3afa committing some missing files
koda
parents:
diff changeset
    53
#if !defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)
5033848d3afa committing some missing files
koda
parents:
diff changeset
    54
#warning __BIG_ENDIAN__ or __LITTLE_ENDIAN__ not found, going to set __LITTLE_ENDIAN__ as default
5033848d3afa committing some missing files
koda
parents:
diff changeset
    55
#define __LITTLE_ENDIAN__ 1
5033848d3afa committing some missing files
koda
parents:
diff changeset
    56
//#error Do not know the endianess of this architecture
5033848d3afa committing some missing files
koda
parents:
diff changeset
    57
#endif
5033848d3afa committing some missing files
koda
parents:
diff changeset
    58
5033848d3afa committing some missing files
koda
parents:
diff changeset
    59
/* use byteswap macros from the host system, hopefully optimized ones ;-) 
5033848d3afa committing some missing files
koda
parents:
diff changeset
    60
 * or define our own version, simple, stupid, straight-forward... */
5033848d3afa committing some missing files
koda
parents:
diff changeset
    61
#ifdef HAVE_BYTESWAP_H
5033848d3afa committing some missing files
koda
parents:
diff changeset
    62
#include <byteswap.h>
5033848d3afa committing some missing files
koda
parents:
diff changeset
    63
#else        
5033848d3afa committing some missing files
koda
parents:
diff changeset
    64
#define bswap_16(x)	((((x) & 0xFF00) >> 8) | (((x) & 0x00FF) << 8))
5033848d3afa committing some missing files
koda
parents:
diff changeset
    65
#define bswap_32(x)	((((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8)  | \
5033848d3afa committing some missing files
koda
parents:
diff changeset
    66
                         (((x) & 0x0000FF00) << 8)  | (((x) & 0x000000FF) << 24) )
5033848d3afa committing some missing files
koda
parents:
diff changeset
    67
#endif /* HAVE_BYTESWAP_H */
5033848d3afa committing some missing files
koda
parents:
diff changeset
    68
5033848d3afa committing some missing files
koda
parents:
diff changeset
    69
/* swap numbers accordingly to architecture automatically */
5033848d3afa committing some missing files
koda
parents:
diff changeset
    70
#ifdef __LITTLE_ENDIAN__
5033848d3afa committing some missing files
koda
parents:
diff changeset
    71
#define ENDIAN_LITTLE_32(x) x
5033848d3afa committing some missing files
koda
parents:
diff changeset
    72
#define ENDIAN_BIG_32(x)    bswap_32(x)
5033848d3afa committing some missing files
koda
parents:
diff changeset
    73
#define ENDIAN_LITTLE_16(x) x
5033848d3afa committing some missing files
koda
parents:
diff changeset
    74
#define ENDIAN_BIG_16(x)    bswap_16(x)
5033848d3afa committing some missing files
koda
parents:
diff changeset
    75
#elif __BIG_ENDIAN__
5033848d3afa committing some missing files
koda
parents:
diff changeset
    76
#define ENDIAN_LITTLE_32(x) bswap_32(x)
5033848d3afa committing some missing files
koda
parents:
diff changeset
    77
#define ENDIAN_BIG_32(x)    x
5033848d3afa committing some missing files
koda
parents:
diff changeset
    78
#define ENDIAN_LITTLE_16(x) bswap_16(x)
5033848d3afa committing some missing files
koda
parents:
diff changeset
    79
#define ENDIAN_BIG_16(x)    x    
5033848d3afa committing some missing files
koda
parents:
diff changeset
    80
#endif
5033848d3afa committing some missing files
koda
parents:
diff changeset
    81
2454
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
    82
/** 1.0 02/03/10 - Defines cross-platform sleep, usleep, etc. [Wu Yongwei] **/
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
    83
#ifndef _SLEEP_H
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
    84
#define _SLEEP_H
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
    85
#ifdef _WIN32
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
    86
# if defined(_NEED_SLEEP_ONLY) && (defined(_MSC_VER) || defined(__MINGW32__))
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
    87
#  include <stdlib.h>
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
    88
#  define sleep(t) _sleep((t) * 1000)
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
    89
# else
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
    90
#  define WIN32_LEAN_AND_MEAN
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
    91
#  include <windows.h>
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
    92
#  define sleep(t)  Sleep((t) * 1000)
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
    93
# endif
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
    94
# ifndef _NEED_SLEEP_ONLY
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
    95
#  define msleep(t) Sleep(t)
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
    96
#  define usleep(t) Sleep((t) / 1000)
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
    97
# endif
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
    98
#else
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
    99
# include <unistd.h>
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
   100
# ifndef _NEED_SLEEP_ONLY
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
   101
#  define msleep(t) usleep((t) * 1000)
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
   102
# endif
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
   103
#endif
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
   104
#endif /* _SLEEP_H */
c8b1fb10003c fix a potential bug when playing two sounds at the same time (or very near)
koda
parents: 2445
diff changeset
   105
2445
5033848d3afa committing some missing files
koda
parents:
diff changeset
   106
#pragma pack(1)
5033848d3afa committing some missing files
koda
parents:
diff changeset
   107
typedef struct _WAV_header_t {
5033848d3afa committing some missing files
koda
parents:
diff changeset
   108
        uint32_t ChunkID;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   109
        uint32_t ChunkSize;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   110
        uint32_t Format;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   111
        uint32_t Subchunk1ID;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   112
        uint32_t Subchunk1Size;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   113
        uint16_t AudioFormat;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   114
        uint16_t NumChannels;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   115
        uint32_t SampleRate;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   116
        uint32_t ByteRate;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   117
        uint16_t BlockAlign;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   118
        uint16_t BitsPerSample;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   119
        uint32_t Subchunk2ID;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   120
        uint32_t Subchunk2Size;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   121
} WAV_header_t;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   122
#pragma pack()
5033848d3afa committing some missing files
koda
parents:
diff changeset
   123
5033848d3afa committing some missing files
koda
parents:
diff changeset
   124
#pragma pack(1)
5033848d3afa committing some missing files
koda
parents:
diff changeset
   125
typedef struct _SSound_t {
5033848d3afa committing some missing files
koda
parents:
diff changeset
   126
        int source;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   127
        char Filename[256];
5033848d3afa committing some missing files
koda
parents:
diff changeset
   128
        ALuint Buffer;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   129
} SSound_t;
5033848d3afa committing some missing files
koda
parents:
diff changeset
   130
#pragma pack()
5033848d3afa committing some missing files
koda
parents:
diff changeset
   131
5033848d3afa committing some missing files
koda
parents:
diff changeset
   132
#endif
5033848d3afa committing some missing files
koda
parents:
diff changeset
   133