File: Sound.cpp

package info (click to toggle)
gav 0.9.0-3
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, lenny, sid, squeeze, stretch, wheezy
  • size: 660 kB
  • ctags: 814
  • sloc: cpp: 4,401; makefile: 147; sh: 15
file content (99 lines) | stat: -rw-r--r-- 2,276 bytes parent folder | download | duplicates (3)
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
/* -*- C++ -*- */
/*
  GAV - Gpl Arcade Volleyball
  
  Copyright (C) 2002
  GAV team (http://sourceforge.net/projects/gav/)

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "Sound.h"
#include <string.h>

#ifdef AUDIO

int Sound::loadAndConvertSound(const char *filename, SDL_AudioSpec *spec,sound_p sound)
{
  SDL_AudioCVT cvt;
  SDL_AudioSpec loaded;
  Uint8 *new_buf;
  
  // printf("loading sound: %s\n", filename);

  if(SDL_LoadWAV(filename, &loaded,&sound->samples, &sound->length)==NULL) {
    printf("Cannot load the wav file %s\n", filename);
    return 1;
  }

  if (SDL_BuildAudioCVT(&cvt, loaded.format, loaded.channels,loaded.freq,spec->format,
			spec->channels,spec->freq)<0)
    {
      printf("Cannot convert the sound file %s\n", filename);
    }

  cvt.len =sound->length;
  new_buf = (Uint8 *) malloc(cvt.len*cvt.len_mult);

  memcpy(new_buf,sound->samples,sound->length);

  cvt.buf = new_buf;

  if(SDL_ConvertAudio(&cvt)<0) {
    printf("Audio conversion failed for file %s\n", filename);
    free(new_buf);
    SDL_FreeWAV(sound->samples);
    return 1;
  }
  
  SDL_FreeWAV(sound->samples);
  sound->samples = new_buf;

  sound->length = sound->length * cvt.len_mult;

  return 0;
}



int Sound::playSound(bool loop)
{
  int i;

  for(i=0;i<MAX_PLAYING_SOUNDS;i++)
    {
      if(playing[i].active==0)
	break;
    }

  if(i==MAX_PLAYING_SOUNDS)
    return 1;

  id = i;

  SDL_LockAudio();
  playing[i].active=1;
  playing[i].sound=&this_sound;
  playing[i].position=0;
  playing[i].loop = loop;
  SDL_UnlockAudio();
  return 0;
}

void Sound::stopSound() {
  playing[id].active = 0;
}

#endif //AUDIO