<commit_before>#include "QxtAVFile.h"
#include "QxtAVPlayer.h"
#include "QxtAVPlayer_p.h"
#include <QxtCore/QxtHyperMacros>
#include <limits> 

/**
\class QxtAVPlayer QxtAVPlayer

\ingroup media

\brief simple player using the QxtAVFile and SDL

example:
\code 
QxtAudioPlayer player;
player.play("foo.wav");
\endcode
*/

//------------------------------------------------------------------------------------



static void Callback (void * userData, Uint8 *stream, int size)
	{
	short *out = (short*)stream;
	int length = size/sizeof(short);
	QxtAVPlayerPrivate * player =  (QxtAVPlayerPrivate *)userData;

	float * peek =player->peek;
	bool peeking= player->peeking;
	if(player->avfile)
		{
		short a[size];
		short *pa=a;

		QXT_DROP_SCOPE(e,player->avfile->read(a, length))
			{
			player->up_fetch_eof();
			}

		fortimes(length)
			{
			if (peeking)
				(*peek++)=(float)*pa/ (float)std::numeric_limits<short>::max();

 			(*out++) =(short)((*pa++)*0.9*player->volume_m);

			}
		}

	else
		fortimes(length)
			*out++=0;
	}


//------------------------------------------------------------------------------------


QxtAVPlayerPrivate::QxtAVPlayerPrivate()
	{
	avfile=NULL;
	samplerate=48000;
	volume_m=0.99;
	opened_m=false;
	peeking=false;
	peek=new float  [2048*10];
	connect(this,SIGNAL(eof()),this,SLOT(close()));
	}
		
//------------------------------------------------------------------------------------


QxtError QxtAVPlayerPrivate::play(QString file)
	{
	if (opened_m)
		{
		SDL_PauseAudio (1);
		delete avfile;
		avfile= new QxtAVFile();
		QXT_DROP_F(avfile->open(file));
		SDL_PauseAudio (0);
		}
	else
		{
		avfile= new QxtAVFile();
		QXT_DROP_F(avfile->open(file));
		QXT_DROP_F(open());
		}

	QXT_DROP_OK;
	}
//------------------------------------------------------------------------------------

QxtError QxtAVPlayerPrivate::open()
	{
	if (opened_m)	QXT_DROP(Qxt::LogicalError);
	if (!avfile)	QXT_DROP(Qxt::Bug);




	if(SDL_Init (SDL_INIT_AUDIO)<0)QXT_DROP(Qxt::SDLError);

	SDL_AudioSpec wanted_spec;
	wanted_spec.freq=avfile->samplerate();
	wanted_spec.format=AUDIO_S16SYS;
	wanted_spec.channels=2;
        wanted_spec.silence = 0;

	wanted_spec.samples=2048;<commit_msg>emits eof instead of crashing when it cannot play files now<commit_after>#include "QxtAVFile.h"
#include "QxtAVPlayer.h"
#include "QxtAVPlayer_p.h"
#include <QxtCore/QxtHyperMacros>
#include <limits> 

/**
\class QxtAVPlayer QxtAVPlayer

\ingroup media

\brief simple player using the QxtAVFile and SDL

example:
\code 
QxtAudioPlayer player;
player.play("foo.wav");
\endcode
*/

//------------------------------------------------------------------------------------



static void Callback (void * userData, Uint8 *stream, int size)
	{
	short *out = (short*)stream;
	int length = size/sizeof(short);
	QxtAVPlayerPrivate * player =  (QxtAVPlayerPrivate *)userData;

	float * peek =player->peek;
	bool peeking= player->peeking;
	if(player->avfile)
		{
		short a[size];
		short *pa=a;

		QXT_DROP_SCOPE(e,player->avfile->read(a, length))
			{
			player->up_fetch_eof();
			}

		fortimes(length)
			{
			if (peeking)
				(*peek++)=(float)*pa/ (float)std::numeric_limits<short>::max();

 			(*out++) =(short)((*pa++)*0.9*player->volume_m);

			}
		}

	else
		fortimes(length)
			*out++=0;
	}


//------------------------------------------------------------------------------------


QxtAVPlayerPrivate::QxtAVPlayerPrivate()
	{
	avfile=NULL;
	samplerate=48000;
	volume_m=0.99;
	opened_m=false;
	peeking=false;
	peek=new float  [2048*10];
	connect(this,SIGNAL(eof()),this,SLOT(close()));
	}
		
//------------------------------------------------------------------------------------


QxtError QxtAVPlayerPrivate::play(QString file)
	{
	if (opened_m)
		{
		SDL_PauseAudio (1);
		opened_m=false;
		delete avfile;
		avfile= new QxtAVFile();
		QXT_DROP_SCOPE(error,avfile->open(file))
			{
			avfile=NULL;	
		 	SDL_CloseAudio();
			qWarning("error opening File");
			up_fetch_eof();
			QXT_DROP_F(error);
			}
		opened_m=true;
		SDL_PauseAudio (0);

		}
	else
		{
		avfile= new QxtAVFile();
		QXT_DROP_F(avfile->open(file));
		QXT_DROP_F(open());
		}

	QXT_DROP_OK;
	}
//------------------------------------------------------------------------------------

QxtError QxtAVPlayerPrivate::open()
	{
	if (opened_m)	QXT_DROP(Qxt::LogicalError);
	if (!avfile)	QXT_DROP(Qxt::Bug);




	if(SDL_Init (SDL_INIT_AUDIO)<0)QXT_DROP(Qxt::SDLError);

	SDL_AudioSpec wanted_spec;
	wanted_spec.freq=avfile->samplerate();
	wanted_spec.format=AUDIO_S16SYS;
	wanted_spec.channels=2;
        wanted_spec.silence = 0;

	wanted_spec.samples=2048;
{'fraction_non_alphanumeric': 0.2808159722222222, 'fraction_numerical': 0.012586805555555556, 'mean_word_length': 2.3206051873198845, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2852294', 'n_tokens_mistral': 1872, 'n_tokens_neox': 1669, 'n_words': 263}