2 WAV Dateien gleichzeitig abspielen + SDL

Q

qwertz

Hallo, ich habe hier ein Beispiel aus der SDL villeicht kann mir jemand ja helfen, es so hinzubekommen, das beide WAV Dateien gleichzeitig abgespielt werden. Wenn ich 2x wavplay (BSP Prog SDL starte funktioniert es ja auch synchron)

Danke schon im Voraus für alle Tipps: :hilfe2:

Zuerst meine MAIN File
-----------------------------------
#include "playwave.h"
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

void *print_message_function( void *ptr );
int main(){

pthread_t thread1, thread2;
char *message1 = "/home/xyz/testdir/src/1.wav";
char *message2 = "/home/xyz/testdir/src/2.wav";
int iret1, iret2;

/* Create independent threads each of which will execute function */

iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);

//pthread_join( thread1, NULL);
sleep (2);
pthread_join( thread2, NULL);

printf("Thread 1 returns: %d\n",iret1);
printf("Thread 2 returns: %d\n",iret2);
exit(0);
}

void *print_message_function( void *ptr ){
char *message;
message = (char *) ptr;

cl_playwave * player = new cl_playwave(message, 44100, 2);
player->play();
printf("%s \n", message);
}
================================================================
Diese scheint voll zu funktionieren und startet die 2 Threads die 2x die Klasse erstellen und den Sound abspielen
=================================================================





Die playwave.h
-----------------------
#include <iostream>
#include <string.h>
#include <SDL.h>
#include "SDL_mixer.h"

using namespace std;

class cl_playwave{

public:
cl_playwave(string filename, int audio_rate=MIX_DEFAULT_FREQUENCY, int audio_channels=2, Uint16 audio_format=MIX_DEFAULT_FORMAT);
~cl_playwave();
int loadwav(string filename);
int play();
void distroy();
private:
string s_filename;
Mix_Chunk *p_wave;
bool b_audio_open;
int i_audio_rate;
int i_audio_channels;
Uint16 o_audio_format;
};



=================================================================
Und die playwave.cpp

#include "playwave.h"


cl_playwave::cl_playwave(string filename, int audio_rate, int audio_channels, Uint16 audio_format){
cl_playwave::s_filename=filename;
cl_playwave::p_wave = NULL;
cl_playwave::b_audio_open = false;
cl_playwave::i_audio_rate = audio_rate;
cl_playwave::i_audio_channels = audio_channels;
cl_playwave::o_audio_format = audio_format;
if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
cerr << "Couldn't initialize SDL: " << SDL_GetError() << endl;
throw -1;
}
if (Mix_OpenAudio(cl_playwave::i_audio_rate, cl_playwave::o_audio_format, cl_playwave::i_audio_channels, 4496) < 0) {
cerr << "Couldn't open audio: " << SDL_GetError() << endl;
distroy();
throw -2;
} else {
Mix_QuerySpec(&i_audio_rate, &o_audio_format, &i_audio_channels);
}
cl_playwave::b_audio_open = true;
}

int cl_playwave::loadwav(string filename){
cl_playwave::s_filename=filename;
}

int cl_playwave::play(){
/* Load the requested wave file */
cl_playwave::p_wave = Mix_LoadWAV(cl_playwave::s_filename.c_str());
if ( cl_playwave::p_wave == NULL ) {
cerr << "Couldn't load %s: " << cl_playwave::s_filename << SDL_GetError() << endl;
distroy();
return -3;
}

// Play
Mix_PlayChannel(0, cl_playwave::p_wave, 0);

while (Mix_Playing(0)) {

SDL_Delay(1);
//while (cl_playwave::b_pause){ //Pause funktion
//SDL_Delay(1);
//}

}

//distroy();
return 0;

}


//int cl_playwave::pause(bool true_false){
// cl_playwave::b_pause = true_false;
//}

void cl_playwave::distroy(){
if ( cl_playwave::p_wave ) {
Mix_FreeChunk(cl_playwave::p_wave);
cl_playwave::p_wave = NULL;
}
if ( cl_playwave::b_audio_open ) {
Mix_CloseAudio();
cl_playwave::b_audio_open = 0;
}
SDL_Quit();
}

cl_playwave::~cl_playwave(){
distroy();
}
 

Ähnliche Themen

Unix Webserver mit HTML Seite erstellen

Ausführbare C-Datei von Mac OS auf Embedded Linux ausführen

dynamische Speicherreservierung

C HTTP request

Problem mit Texteingabe

Zurück
Oben