summaryrefslogtreecommitdiffstats
path: root/nebu/audio/SourceCopy.cpp
blob: 15c7ca10cc39d52b163835834e7a29adb4168c42 (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
#include "audio/nebu_SourceCopy.h"

#include <assert.h>

namespace Sound {
  int SourceCopy::Mix(Uint8 *data, int len) {
    if(_source->_buffer == NULL) return 0;

    int volume = (int)(_source->GetVolume() * SDL_MIX_MAXVOLUME);
    // fprintf(stderr, "playing copy sample at %d, position: %d\n", volume, _position);
    int buffersize = _source->_buffersize;
    Uint8* buffer = (Uint8*) _source->_buffer;
    
    assert(len < buffersize);
      
    if(len < buffersize - _position) {
      SDL_MixAudio(data, buffer + _position, len, volume);
      _position += len;
    } else { 
      SDL_MixAudio(data, buffer + _position, buffersize - _position,
		   volume);
      len -= buffersize - _position;

      printf("end of sample reached!\n");
      if(_loop) {
	if(_loop != 255) 
	  _loop--;

	_position = 0;
	SDL_MixAudio(data, buffer + _position, len, volume);
	_position += len;
      } else {
	_isPlaying = 0;
      }
    }
    return 1;
  }
}