summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/util/audio/audioIO_SDL.cpp
blob: 782fa3888263070b7ecf42dbbd386b201b111794 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
  audio wrapper for SDL
  Copyright (C) 2000  Martin Vogt

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Library General Public License as published by
  the Free Software Foundation.

  For more information look at the file COPYRIGHT in this package

 */
#include "../../input/bufferInputStream.h"
#include <assert.h>
#include <iostream.h>
#if defined WIN32
#include <SDL.h>
#include <SDL_audio.h>
#else
#include <SDL/SDL.h>
#include <SDL/SDL_audio.h>
#endif

//static  SDL_AudioSpec actual;
static  BufferInputStream* audioRing;
static  TimeStamp* dummy;
static  int lOpen=false;



int audioConstruct() {
  cout << "audioConstruct ********* SDL"<<endl;
  if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
    fprintf(stderr, "Warning: Couldn't init SDL audio: %s\n",
	    SDL_GetError());
    exit(0);
  }
  atexit(SDL_Quit);
  audioRing=new BufferInputStream(1024*65,1024*8,"audioSDL");
  audioRing->open("audioSDL");
  dummy=new TimeStamp();
  lOpen=false;
  return true;
}


void audioDestruct() {
  delete audioRing;
  delete dummy;
}




int audioOpen() {
  return true;
}


void audioClose() {
  lOpen=false;
  SDL_CloseAudio();
}


void audioCallback(void *, Uint8 *stream, int len) {
  char* startPtr;
  TimeStamp* start;
  int bytePos;

  int read=audioRing->readRemote(&startPtr,len);
  SDL_MixAudio(stream, (Uint8*) startPtr, read, SDL_MIX_MAXVOLUME);
  
  audioRing->forwardReadPtr(read);
  // dequeue time stamps
  bytePos=audioRing->getBytePosition();
  start=audioRing->getTimeStamp(bytePos);

}



void audioInit(int sampleSize,int frequency, int stereo, int sign, int big) {
  if( sign == 0 )
  {
      fprintf(stderr,
              "%s, %d: expecting signed audio data, "
              "initialized unsigned (ignored)\n",
              __FILE__, __LINE__ );
  }
  if( big != 0 )
  {
      fprintf(stderr,
              "%s, %d: expecting little endian audio data, "
              "initialized big endian (ignored)\n",
              __FILE__, __LINE__ );
  }

  cout << "SDL audioInit: "
       << " sampleSize:"<<sampleSize
       << " frequency:"<<frequency
       << " stereo:"<<stereo<<endl;
  if (lOpen==true) {
    cout << "SDL is buggy, because open != init -> return"<<endl;
    return;
  }
  lOpen=true;
  SDL_AudioSpec wanted;
  //SDL_AudioSpec actual;
  if (sampleSize == 16) {
    wanted.format= AUDIO_S16LSB;
  } else {
    wanted.format= AUDIO_S8;
  }

  wanted.freq=frequency;
  wanted.channels=stereo+1;
  wanted.samples = 1024;
  wanted.callback = audioCallback;
  wanted.userdata = NULL;

  int err=SDL_OpenAudio(&wanted, NULL);
  if (err != 0) {
    cout << "SDL_OpenAudio not ok"<<endl;
    cout << "error is:"<<SDL_GetError()<<endl;
    exit(0);
  }
  SDL_PauseAudio(0);

}


int mixerOpen() {
  return true;
}


void mixerClose() {
}


void mixerSetVolume(int volumeLeft,int volumeRight) {
  cout << "volumeLeft:"<<volumeLeft
       << " volumeRight:"<<volumeRight<<endl;
}


int audioWrite(char *buffer, int count) {

  audioRing->write(buffer,count,dummy);

  
  return count;
}


int getAudioFd() {
  return false;
}


int getAudioBufferSize() {
  int buf=1024*65;
  return buf;
}