playing with sounds... needs a lot of work

git-svn-id: svn://anubis/anaglym/trunk@278 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
Josh Holtrop 2010-06-16 04:29:58 +00:00
parent 6e502367bc
commit 908a860482
4 changed files with 70 additions and 9 deletions

58
AV.cc
View File

@ -114,15 +114,30 @@ void AV::stop()
}
}
refptr<AV::Sound> AV::createSound()
{
refptr<Sound> s = new Sound(*this);
#if 0
m_sounds.insert(s);
#endif
return s;
}
void AV::playSound(AV::Sound * s)
{
m_active_sounds.insert(s);
}
void AV::playCallback(Uint8 * stream, int len)
{
static int pos = 0;
Sint16 * str = (Sint16 *) stream;
for (int i = 0; i < len / 4; i++)
for (std::set<Sound *>::const_iterator it = m_active_sounds.begin();
it != m_active_sounds.end();
it++)
{
str[i*2] = (Sint16) (32000.0 * sin(pos * 440.0 / AV_SOUND_RATE * 2.0 * M_PI));
str[i*2+1] = 0;
pos++;
if ((*it)->decode(stream, len) == 0)
{
m_active_sounds.erase(it);
}
}
}
@ -166,3 +181,34 @@ bool AV::Sound::load(FileLoader & fileLoader, const FileLoader::Path & path)
}
return false;
}
void AV::Sound::play()
{
if (m_ss != NULL)
{
#if 0
cerr << "play()" << endl;
Sound_Rewind(m_ss);
cerr << "end play()" << endl;
#endif
m_av.playSound(this);
}
}
void AV::Sound::stop()
{
}
void AV::Sound::loop(int times)
{
}
int AV::Sound::decode(Uint8 * stream, int len)
{
/* TODO: fix for len != m_ss->buffer_size */
cerr << "AV::Sound::decode()" << endl;
Uint32 len_decoded = Sound_Decode(m_ss);
cerr << "AV::Sound::decode() len: " << len << ", len_decoded: " << len_decoded << endl;
memcpy(stream, m_ss->buffer, len_decoded);
return len_decoded;
}

18
AV.h
View File

@ -2,11 +2,14 @@
#ifndef VIDEO_H
#define VIDEO_H
#include "FileLoader/FileLoader.h"
#include "refptr/refptr.h"
#include <SDL.h>
#include <SDL_sound.h>
#include <set>
#include "FileLoader/FileLoader.h"
#include "refptr/refptr.h"
#define AV_SOUND_RATE 44100
class AV
@ -19,6 +22,10 @@ class AV
~Sound();
bool load(FileLoader & fileLoader,
const FileLoader::Path & path);
void play();
void stop();
void loop(int times);
int decode(Uint8 * stream, int len);
protected:
AV & m_av;
SDL_RWops * m_rwops;
@ -53,7 +60,8 @@ class AV
{
return SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON;
}
refptr<Sound> createSound() { return new Sound(*this); }
refptr<Sound> createSound();
void playSound(Sound * s);
friend void AV_sound_callback(void * userdata, Uint8 * stream, int len);
@ -66,6 +74,10 @@ class AV
int m_height;
SDL_Surface * m_surface;
bool m_fullscreen;
#if 0
std::set< refptr<Sound> > m_sounds;
#endif
std::set< Sound * > m_active_sounds;
};
#endif

View File

@ -571,6 +571,7 @@ int Engine::loadSound(const string & name)
if (avs->load(*m_fileLoader, path))
{
int id = addSound(avs);
avs->play();
return id;
}
else

View File

@ -38,6 +38,8 @@ function key_down_event(key)
ag.registerEventHandler(mouse_moves, "mouse_motion")
elseif (key == "n") then
ag.clearEventHandler("mouse_motion")
elseif (key == "s") then
ag.loadSound("iris.mp3")
end
end