Commit a37a58a3 authored by daFischer's avatar daFischer

Update (halfway)

parent 229c02d8
/*
* File: Audio.cpp
* Author: user
*
* http://wiki.delphigl.com/index.php/OpenAL-Funktions%C3%BCbersicht
* Created on April 30, 2014, 3:39 PM
*/
......@@ -10,110 +10,180 @@
Audio::Audio(const char* path) {
//settings
audio_rate = 22050;
audio_format = AUDIO_S16SYS;
audio_channels = 2;
audio_buffers = 4096;
musicPlaying=0;
failed=false;
finished=true;//TODO: false
playing=0;
failed=true;
int error;
struct stat statbuf;
if(!(device = alcOpenDevice(NULL)))
{
printf("no device\n");
return;
}
if(!(context = alcCreateContext(device, NULL)))
{
printf("no context\n");
return;
}
if(!alcMakeContextCurrent(context))
{
printf("no current\n");
return;
}
if((error = alcGetError(device)) != ALC_NO_ERROR)
{
printf("alc error: %d\n", error);
return;
}
//Initialize SDL_mixer with settings as above
if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) != 0)
alGenBuffers(1, &buffer); //Generate buffer
alGenSources(1, &source); //Generate source
if(alGetError() != AL_NO_ERROR)
{
printf("Unable to initialize audio: %s\n", Mix_GetError());
failed=true;
printf("Error (Audio)\n");
return;
}
/*
//Load audio file
string extension[6] = {".mp3", ".MP3", ".mp2", ".MP2", ".ogg"};//.wav don't work
//Load our audio file from disk
string extension[4] = {".mp3", ".mp2", ".ogg", ".wav"};
ALboolean loop=false;
string filename;
for(int i=0;i<sizeof(extension);i++)
for(int i=0;i<sizeof(extension);i++) //try loading audio of all possible extensions
{
filename=path+extension[i];
music = Mix_LoadMUS(filename.c_str());
if(music == NULL)
if(readAudio(filename.c_str(),extension[i]))
{
printf("No %s file loaded: %s\n", extension[i].c_str(), Mix_GetError());
}
else
printf("loaded %s\n",filename.c_str());
break;
}
if(i == sizeof(extension) -1)
alDeleteBuffers(1, &buffer);
}
if(music == NULL)
//alBufferData(buffer,format,data,size,frequency); //fill buffer with audio data
alSourcei (source, AL_BUFFER, buffer);
if (alGetError() != AL_NO_ERROR)
{
printf("Unable to load audio file.\n");
failed=true;
printf("Audio last error\n");
return;
}
*/
failed=false;
}
void Audio::play(){
//Play the audio file
/*if(Mix_PlayMusic(music, 0) == -1)
alSourcePlay(source);
printf("Start playing for %d ms\n",getDuration());
/*if(Mix_PlayMusic(audio, 0) == -1)
{
printf("Unable to play audio file: %s\n", Mix_GetError());
return;
}*/
//The music is playing!
musicPlaying = 1;
//The audio is playing!
playing = 1;
startTime=0;
//startTime=0;
/*printf("Setting time\n");
if(setPosition(30)==0)
printf("Unable to set Position\n");*/
//Make sure that the finished() function is called when the music stops playing
Mix_HookMusicFinished(audioFinished);
//Make sure that the finished() function is called when the audio stops playing
//Mix_HookMusicFinished(audioFinished);
}
Audio::~Audio() {
//Release the memory allocated to our music
Mix_HaltMusic();
Mix_FreeMusic(music);
//Need to make sure that SDL_mixer and SDL have a chance to clean up
Mix_CloseAudio();
bool Audio::readAudio(const char* path, string type){
struct stat statbuf;
if(stat(path, &statbuf) != 0 || !S_ISREG(statbuf.st_mode))
{
printf("No %s file found.\n", type.c_str());
continue;
}
int error;
switch(type)
{
case ".mp3":
return false;
break;
case ".mp2":
return false;
break;
case ".ogg":
return false;
break;
case ".wav":
FILE* f = fopen (path , "r");
alBufferData(buffer,format,data,size,freq);
if((error = alGetError()) != AL_NO_ERROR)
{
printf("No %s file loaded: %d\n", type.c_str(), error);
continue;
}
break;
}
}
static void audioFinished()
/*static void audioFinished()
{
printf("Finished the Audio\n");
}
/*void Audio::finished()
void Audio::finished()
{
//Music is done!
//Audio is done!
printf("Finished the Audio2\n");
musicPlaying = 0;
playing = 0;
}*/
int Audio::getTime()
{
//might help: http://stackoverflow.com/questions/10110905/simple-wave-generator-with-sdl-in-c
currentTime=SDL_GetTicks();
if(startTime==0)
startTime=currentTime;
//currentTime-=startTime;
//printf("%d\n",currentTime);
return currentTime;
ALfloat time;
alGetSourcef(source, AL_SEC_OFFSET, &time);
if(alGetError()!= AL_NO_ERROR)
printf("getTime error\n");
//printf("%f\n",(float) time);
return (int)(time*1000);
}
int Audio::setPosition(double pos)
bool Audio::setPosition(int time)
{
return Mix_SetMusicPosition(pos);
ALfloat pos=(ALfloat)pos/1000.0;
alSourcef(source, AL_SEC_OFFSET, pos);
if(alGetError()== AL_NO_ERROR)
return true;
else
return false;
}
double Audio::getDuration()
int Audio::getDuration()
{
//not possible with sdl, try openAL?
return 0.0;
ALint numBytes;
ALint channels;
ALint bits;
alGetBufferi(buffer, AL_SIZE, &numBytes);
alGetBufferi(buffer, AL_CHANNELS, &channels);
alGetBufferi(buffer, AL_BITS, &bits);
int numSamples = numBytes * 8 / (channels * bits);
ALint frequency;
alGetBufferi(buffer, AL_FREQUENCY, &frequency);
double seconds = (double)numSamples / (double)frequency;
if(alGetError()!= AL_NO_ERROR)
printf("getDuration error\n");
return (int)(seconds*1000);
}
......@@ -11,9 +11,12 @@
#include <stdio.h>
#include <time.h>
#include <string>
#include <sys/stat.h>
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
#include <AL/al.h>
#include <AL/alut.h>
#define BUFFER_SIZE 4096
using namespace std;
......@@ -23,23 +26,32 @@ class Audio {
public:
Audio(const char*);
void play();
virtual ~Audio();
//void finished();
void finished();
int getTime();
int setPosition(double pos);
double getDuration();
bool setPosition(int time);
int getDuration();
bool failed;
bool finished;
private:
int audio_rate; //Frequency of audio playback
Uint16 audio_format; //Format of the audio we're playing
int audio_channels; //2 channels = stereo
int audio_buffers; //Size of the audio buffers in memory
int musicPlaying;
Mix_Music *music; //Pointer to our music, in memory
int startTime;
int currentTime;
bool readAudio(const char* path, string type);
ALuint source;
ALuint buffer;
ALCdevice *device;
ALCcontext *context;
//ALsizei size;
//ALsizei frequency; //Frequency of audio playback
//ALenum format; //Format of the audio we're playing
//ALvoid* data;
//unsigned char *buf;
//int pid;
//int files[2];
//FILE *f;
int playing;
//int startTime;
//int currentTime;
};
#endif /* AUDIO_H */
......
......@@ -2,17 +2,16 @@
* File: Inflater.cpp
* Author: user
* using http://zlib.net/zlib_how.html
* using http://curl.haxx.se/libcurl/c/fopen.html
*
* Created on May 8, 2014, 12:46 PM
*/
#include "Inflater.h"
Inflater::Inflater(Downloader* d)
Inflater::Inflater(FILE* f)
{
//printf("Constructor of Inflater\n");
source=d;
source=f;
outOffset=0;
// allocate inflate state
......@@ -28,8 +27,8 @@ Inflater::Inflater(Downloader* d)
return;
}
strm.avail_in = source->read((char*)in, CHUNK); //from the outer loop of the example
if (source->error) {
strm.avail_in = fread(in, 1, CHUNK, source); //from the outer loop of the example
if (ferror(source)) {
(void)inflateEnd(&strm);
ret = Z_ERRNO;
}
......@@ -84,8 +83,8 @@ bool Inflater::readByte(char* Byte)
if(strm.avail_out != 0) //Array in has to be refilled
{
//printf("Refill Array in\n");
strm.avail_in = source->read((char*)in, CHUNK);
if (source->error) {
strm.avail_in = fread(in, 1, CHUNK, source);
if (ferror(source)) {
(void)inflateEnd(&strm);
return ret = Z_ERRNO;
}
......
......@@ -13,10 +13,7 @@
#include <fstream>
#include <zlib.h>
#include <assert.h>
#include <curl/curl.h>
#include "SizedArray.h"
#include "Downloader.h"
using namespace std;
......@@ -24,7 +21,7 @@ using namespace std;
class Inflater {
public:
Inflater(Downloader* d);
Inflater(FILE* f);
~Inflater();
bool readByte(char* Byte);
bool readSizedArray(SizedArray* sArray);
......@@ -35,7 +32,7 @@ public:
bool endOfFile();
private:
Downloader* source;
FILE* source;
unsigned char in[CHUNK];
unsigned char out[CHUNK];
int outOffset;
......
......@@ -12,7 +12,7 @@ Player *player;
Player::Player(const char* cpath, const char* cfilename) {
paused=false;
progress=0;
player=this;
//Initialize BOTH SDL video and SDL audio
......@@ -25,9 +25,16 @@ Player::Player(const char* cpath, const char* cfilename) {
std::string filename=cfilename;
std::string path=cpath;
downloader = new Downloader((char*) (path+"/"+filename+"_a/"+filename+".ttt").c_str());
video=new Video(downloader);
audioURL=(path+"/"+filename+"_a/"+filename);
audio=new Audio((path+"/"+filename+"_a/"+filename).c_str());
video=new Video((path+"/"+filename+"_a/"+filename+".ttt").c_str());
if(video->failed||audio->failed)
{
printf("Audio failed: %s\nVideo failed: %s\n",audio->failed ? "true" : "false",video->failed ? "true" : "false");
return;
}
audio->play();
printf("start looping\n");
#ifdef EMSCRIPTEN
......@@ -47,46 +54,7 @@ Player::Player(const char* cpath, const char* cfilename) {
void Player::loop()
{
switch(progress)
{
case 0:
if(video->failed)
{
printf("Video failed\n");
progress=-1;
delete(downloader);
}
else
if(video->isReady())
{
printf("Video ready\n");
progress++;
video->startReading();
//downloader->retarget((char*) audioURL.c_str());
audio=new Audio(audioURL.c_str());
}
break;
case 1:
if(audio->failed)
{
printf("Audio failed\n");
progress=-1;
delete(downloader);
}
else
if(downloader->ready)
{
printf("both finished\n");
progress++;
delete(downloader);
audio->play();
video->update(audio->getTime());
}
break;
case 2:
video->update(audio->getTime());
break;
}
video->update(audio->getTime());
SDL_Event event;
while (SDL_PollEvent(&event))
......
......@@ -32,9 +32,6 @@ public:
virtual ~Player();
private:
Downloader* downloader;
string audioURL;
int progress;
Audio *audio;
Video *video;
bool paused;
......
......@@ -6,25 +6,17 @@
*/
#include "Video.h"
#include "Message.h"
Video::Video(Downloader* d) {
Video::Video(const char* path) {
original=true;
failed=false;
finished=false;
downloader = d;
downloader->download();
}
void Video::startReading(){
downloader->read(prefs.versionMsg, 12);
//FILE* f = fopen (path , "r");
//fread(prefs.versionMsg, 1, 12, f);
FILE* f = fopen (path , "r");
fread(prefs.versionMsg, 1, 12, f);
// TODO: test version
Inflater* inflater = new Inflater(downloader);
Inflater* inflater=new Inflater(f);
if(readServerInit(inflater))
printf("Video Initialization success: \n%s\n", prefs.name);
......@@ -60,6 +52,7 @@ void Video::startReading(){
void Video::update(int zeit)
{
//printf("%d\n",zeit);
//printf("%d>=%d || %d>%d\n",currentMessage,numMessages,messages[currentMessage]->timestamp,timestamp);
while(currentMessage<numMessages)
{
......@@ -147,6 +140,3 @@ void Video::readExtensions(Inflater* in){
Video::~Video() {
}
bool Video::isReady(){
return downloader->ready;
}
......@@ -16,7 +16,6 @@
#include <zlib.h>
#include <time.h>
#include <assert.h>
#include <curl/curl.h>
#include "Inflater.h"
#include "ProtocolPreferences.h"
......@@ -29,20 +28,14 @@ using namespace std;
class Video {
public:
Video(Downloader*);
Video(const char* path);
//Video(const Video& orig);
virtual ~Video();
void startReading();
bool isReady();
void update(int zeit);
bool failed;
bool finished;
private:
void ready();
Downloader* downloader;
private:
SDL_Surface *screen; //Pointer to the main screen surface
Message** messages;
int numMessages;
......
build/Debug/GNU-Linux-x86/DeleteAllAnnotation.o: DeleteAllAnnotation.cpp \
DeleteAllAnnotation.h Message.h Inflater.h SizedArray.h Downloader.h \
DeleteAllAnnotation.h Message.h Inflater.h SizedArray.h \
ProtocolPreferences.h Constants.h
DeleteAllAnnotation.h:
......@@ -10,8 +10,6 @@ Inflater.h:
SizedArray.h:
Downloader.h:
ProtocolPreferences.h:
Constants.h:
build/Debug/GNU-Linux-x86/EmptyMessage.o: EmptyMessage.cpp EmptyMessage.h \
Message.h Inflater.h SizedArray.h Downloader.h ProtocolPreferences.h \
Constants.h
Message.h Inflater.h SizedArray.h ProtocolPreferences.h Constants.h
EmptyMessage.h:
......@@ -10,8 +9,6 @@ Inflater.h:
SizedArray.h:
Downloader.h:
ProtocolPreferences.h:
Constants.h:
build/Debug/GNU-Linux-x86/HextileMessage.o: HextileMessage.cpp \
HextileMessage.h Inflater.h SizedArray.h Downloader.h Message.h \
ProtocolPreferences.h Constants.h ColorConverter.h
HextileMessage.h Inflater.h SizedArray.h Message.h ProtocolPreferences.h \
Constants.h ColorConverter.h
HextileMessage.h:
......@@ -8,8 +8,6 @@ Inflater.h:
SizedArray.h:
Downloader.h:
Message.h:
ProtocolPreferences.h:
......
build/Debug/GNU-Linux-x86/Inflater.o: Inflater.cpp Inflater.h \
SizedArray.h Downloader.h
SizedArray.h
Inflater.h:
SizedArray.h:
Downloader.h:
build/Debug/GNU-Linux-x86/Message.o: Message.cpp Message.h Inflater.h \
SizedArray.h Downloader.h ProtocolPreferences.h Constants.h \
EmptyMessage.h HextileMessage.h ColorConverter.h WhiteboardMessage.h \
SizedArray.h ProtocolPreferences.h Constants.h EmptyMessage.h \
HextileMessage.h ColorConverter.h WhiteboardMessage.h \
DeleteAllAnnotation.h
Message.h:
......@@ -9,8 +9,6 @@ Inflater.h:
SizedArray.h:
Downloader.h:
ProtocolPreferences.h:
Constants.h:
......
build/Debug/GNU-Linux-x86/Player.o: Player.cpp Player.h Audio.h Video.h \
Inflater.h SizedArray.h Downloader.h ProtocolPreferences.h Message.h \
Constants.h
Inflater.h SizedArray.h ProtocolPreferences.h Message.h Constants.h
Player.h:
......@@ -12,8 +11,6 @@ Inflater.h:
SizedArray.h:
Downloader.h:
ProtocolPreferences.h:
Message.h:
......
build/Debug/GNU-Linux-x86/Video.o: Video.cpp Video.h Inflater.h \
SizedArray.h Downloader.h ProtocolPreferences.h Message.h Constants.h
SizedArray.h ProtocolPreferences.h Message.h Constants.h
Video.h:
......@@ -7,8 +7,6 @@ Inflater.h:
SizedArray.h:
Downloader.h:
ProtocolPreferences.h:
Message.h:
......
build/Debug/GNU-Linux-x86/WhiteboardMessage.o: WhiteboardMessage.cpp \
WhiteboardMessage.h Message.h Inflater.h SizedArray.h Downloader.h \
WhiteboardMessage.h Message.h Inflater.h SizedArray.h \
ProtocolPreferences.h Constants.h
WhiteboardMessage.h:
......@@ -10,8 +10,6 @@ Inflater.h:
SizedArray.h:
Downloader.h:
ProtocolPreferences.h:
Constants.h:
build/Debug/GNU-Linux-x86/main.o: main.cpp main.h Player.h Audio.h \
Video.h Inflater.h SizedArray.h Downloader.h ProtocolPreferences.h \
Message.h Constants.h
Video.h Inflater.h SizedArray.h ProtocolPreferences.h Message.h \
Constants.h
main.h:
......@@ -14,8 +14,6 @@ Inflater.h:
SizedArray.h:
Downloader.h:
ProtocolPreferences.h:
Message.h:
......
......@@ -6,18 +6,18 @@ DIR=../
MESSAGES= $(DIR)DeleteAllAnnotation.cpp $(DIR)EmptyMessage.cpp $(DIR)HextileMessage.cpp $(DIR)Message.cpp $(DIR)WhiteboardMessage.cpp
ZSRC = libz/adler32.c libz/compress.c libz/crc32.c libz/deflate.c libz/gzclose.c libz/gzlib.c libz/gzread.c \
libz/gzwrite.c libz/infback.c libz/inffast.c libz/inflate.c libz/inftrees.c libz/trees.c libz/uncompr.c libz/zutil.c
SRC= $(DIR)Audio.cpp $(DIR)ColorConverter.cpp $(DIR)Constants.cpp $(DIR)Downloader.cpp $(DIR)Inflater.cpp $(DIR)Player.cpp $(DIR)ProtocolPreferences.cpp $(DIR)SizedArray.cpp $(DIR)Video.cpp $(DIR)main.cpp $(MESSAGES)
SRC= $(DIR)Audio.cpp $(DIR)ColorConverter.cpp $(DIR)Constants.cpp $(DIR)Inflater.cpp $(DIR)Player.cpp $(DIR)ProtocolPreferences.cpp $(DIR)SizedArray.cpp $(DIR)Video.cpp $(DIR)main.cpp $(MESSAGES)
EXPORT=-s "EXPORTED_FUNCTIONS=['_main', '_on_play', '_on_pause', '_set_path']"
PRELOAD= --preload-file Diskrete_Strukturen_2013_11_21_a/Diskrete_Strukturen_2013_11_21.mp3 --preload-file Diskrete_Strukturen_2013_11_21_a/Diskrete_Strukturen_2013_11_21.ttt
PRELOAD= --preload-file TTT/Diskrete_Strukturen_2013_11_21_a/Diskrete_Strukturen_2013_11_21.ttt --preload-file TTT/Diskrete_Strukturen_2013_11_21_a/Diskrete_Strukturen_2013_11_21.mp3
#STATICLIB= libz.bc
CFLAGS=-O
TEST_LDFLAGS=-L. libz.a
fileplayer.html: $(SRC)
$(EM) $(SRC) $(EXPORT) -o $@ $(PRELOAD) libz.bc libcurl.la
fileplayer.html: $(SRC) pre.js
$(EM) $(SRC) $(EXPORT) -o $@ $(PRELOAD) libz.bc #--pre-js pre.js
libz.bc: $(ZSRC)
$(CC) $(ZSRC) -s EXPORT_ALL=1 -o $@
......
......@@ -6,18 +6,18 @@ DIR=../
MESSAGES= $(DIR)DeleteAllAnnotation.cpp $(DIR)EmptyMessage.cpp $(DIR)HextileMessage.cpp $(DIR)Message.cpp $(DIR)WhiteboardMessage.cpp
ZSRC = libz/adler32.c libz/compress.c libz/crc32.c libz/deflate.c libz/gzclose.c libz/gzlib.c libz/gzread.c \
libz/gzwrite.c libz/infback.c libz/inffast.c libz/inflate.c libz/inftrees.c libz/trees.c libz/uncompr.c libz/zutil.c
SRC= $(DIR)Audio.cpp $(DIR)ColorConverter.cpp $(DIR)Constants.cpp $(DIR)Downloader.cpp $(DIR)Inflater.cpp $(DIR)Player.cpp $(DIR)ProtocolPreferences.cpp $(DIR)SizedArray.cpp $(DIR)Video.cpp $(DIR)main.cpp $(MESSAGES)
SRC= $(DIR)Audio.cpp $(DIR)ColorConverter.cpp $(DIR)Constants.cpp $(DIR)Inflater.cpp $(DIR)Player.cpp $(DIR)ProtocolPreferences.cpp $(DIR)SizedArray.cpp $(DIR)Video.cpp $(DIR)main.cpp $(MESSAGES)
EXPORT=-s "EXPORTED_FUNCTIONS=['_main', '_on_play', '_on_pause', '_set_path']"
PRELOAD= --preload-file Diskrete_Strukturen_2013_11_21_a/Diskrete_Strukturen_2013_11_21.mp3 --preload-file Diskrete_Strukturen_2013_11_21_a/Diskrete_Strukturen_2013_11_21.ttt
PRELOAD= --preload-file TTT/Diskrete_Strukturen_2013_11_21_a/Diskrete_Strukturen_2013_11_21.ttt --embed-file TTT/Diskrete_Strukturen_2013_11_21_a/Diskrete_Strukturen_2013_11_21.mp3
#STATICLIB= libz.bc
CFLAGS=-O
TEST_LDFLAGS=-L. libz.a
fileplayer.html: $(SRC)
$(EM) $(SRC) $(EXPORT) -o $@ $(PRELOAD) libz.bc
fileplayer.html: $(SRC) pre.js
$(EM) $(SRC) $(EXPORT) -o $@ $(PRELOAD) libz.bc #--pre-js pre.js
libz.bc: $(ZSRC)
$(CC) $(ZSRC) -s EXPORT_ALL=1 -o $@