begin process at 2012 02 09 07:27:52
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Multimédia

 > [ FMODEX | WXWIDGETS ] CAPTURER L'OUTPUT AUDIO + ÉCRITURE DANS UN WAV

[ FMODEX | WXWIDGETS ] CAPTURER L'OUTPUT AUDIO + ÉCRITURE DANS UN WAV


 Information sur la source

Note :
Aucune note
Catégorie :Multimédia Classé sous :fmodex, wxwidgets, capture, waveout Niveau :Débutant Date de création :11/04/2006 Vu / téléchargé :7 627 / 676

Auteur : satellite34

Ecrire un message privé
Site perso
Commentaire sur cette source (2)
Ajouter un commentaire et/ou une note

 Description

voila, suite a une grosse prise de tete pour effectuer cette opération, je vous propose ce morceau de code qui en ravira plus d' un j' espere;

l'enregistrement est limité a 30s mais on peut en changer ( c'est pour les user du site dont je fais partie, pas plus de 30s sinon taitai la SACEM ), un fichier wav est écrit en sortie de programme, il contient la capture enregistrée;

voila, c'est juste un mini exemple, pas un projet, je me garde les mp3 et tout le tralala, lol;

Enjoy

Source

  • #include "recorder.h"
  • #include <cstdio>
  • #define __PACKED
  • MyApp::MyApp()
  • {
  • }
  • bool MyApp::OnInit()
  • {
  • MyFrame *frame = new MyFrame;
  • frame->Show(true);
  • return true;
  • }
  • IMPLEMENT_APP(MyApp)
  • BEGIN_EVENT_TABLE(MyFrame, wxFrame)
  • EVT_CLOSE(MyFrame::OnClose)
  • EVT_TIMER(wxID_ANY, MyFrame::OnTimer)
  • EVT_BUTTON(wxID_ANY, MyFrame::OnClickStart)
  • END_EVENT_TABLE()
  • MyFrame::MyFrame() : wxFrame( NULL,
  • wxID_ANY,
  • _T("Spectrum"),
  • wxDefaultPosition,
  • wxSize(220, 100),
  • wxCAPTION |
  • wxTHICK_FRAME |
  • wxMINIMIZE_BOX |
  • wxSTAY_ON_TOP |
  • wxSYSTEM_MENU |
  • wxCLOSE_BOX |
  • wxFRAME_TOOL_WINDOW )
  • {
  • StartBtn = new wxButton(this, wxID_ANY, "START");
  • InitSoundSystem();
  • timer.SetOwner(this, wxID_ANY);
  • timer.Start( 10 );
  • }
  • MyFrame::~MyFrame(void)
  • {
  • }
  • void MyFrame::InitSoundSystem(void)
  • {
  • sound = NULL;
  • system = NULL;
  • result = FMOD::System_Create(&system);
  • if (result != FMOD_OK)
  • {
  • wxLogError("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
  • return;
  • }
  • result = system->setDriver(0);
  • if(result != FMOD_OK)
  • {
  • wxLogError("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
  • return;
  • }
  • result = system->setSoftwareChannels(100);
  • if(result != FMOD_OK)
  • {
  • wxLogError("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
  • return;
  • }
  • result = system->setHardwareChannels(32, 64, 32, 64);
  • if( result != FMOD_OK )
  • {
  • wxLogError("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
  • return;
  • }
  • result = system->setOutput(FMOD_OUTPUTTYPE_DSOUND);
  • if (result != FMOD_OK)
  • {
  • wxLogError("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
  • return;
  • }
  • result = system->init(200, FMOD_INIT_NORMAL, NULL);
  • if (result != FMOD_OK)
  • {
  • wxLogError("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
  • return;
  • }
  • result = system->setRecordDriver(0);
  • if(result != FMOD_OK)
  • {
  • wxLogError("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
  • return;
  • }
  • char name[256];
  • result = system->getRecordDriverName(0, name, 256);
  • wxLogDebug( name );
  • memset(&exinfo, 0, sizeof(FMOD_CREATESOUNDEXINFO));
  • exinfo.cbsize = sizeof(FMOD_CREATESOUNDEXINFO);
  • exinfo.numchannels = 1;
  • exinfo.format = FMOD_SOUND_FORMAT_PCM16;
  • exinfo.defaultfrequency = 44100;
  • exinfo.length = exinfo.defaultfrequency * sizeof(short) * exinfo.numchannels * 30;// 30 s
  • result = system->createSound(0, FMOD_2D | FMOD_SOFTWARE | FMOD_OPENUSER, &exinfo, &sound);
  • if (result != FMOD_OK)
  • {
  • wxLogError("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
  • return;
  • }
  • }
  • void MyFrame::OnClose(wxCloseEvent &WXUNUSED(event))
  • {
  • system->recordStop();
  • timer.Stop();
  • SaveToWav( sound );
  • Destroy();
  • }
  • void MyFrame::OnTimer(wxTimerEvent &WXUNUSED(event))
  • {
  • unsigned int recordpos = 0;
  • unsigned int length;
  • sound->getLength(&length, FMOD_TIMEUNIT_PCM);
  • system->getRecordPosition(&recordpos);
  • wxString str;
  • str.Printf("durée :%i s, pos: %i s", length, recordpos);
  • wxLogDebug(str);
  • SetTitle( str );
  • }
  • void MyFrame::OnClickStart(wxCommandEvent &WXUNUSED(event))
  • {
  • result = system->recordStart(sound, false);
  • if (result != FMOD_OK)
  • {
  • wxLogError("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
  • return;
  • }
  • sound->setMode(FMOD_LOOP_NORMAL);
  • }
  • // Fonction empruntée a FMOD ( thanks )
  • void MyFrame::SaveToWav(FMOD::Sound *sound)
  • {
  • FILE *fp;
  • int channels, bits;
  • float rate;
  • void *ptr1, *ptr2;
  • unsigned int lenbytes, len1, len2;
  • if (!sound)
  • {
  • return;
  • }
  • sound->getFormat (0, 0, &channels, &bits);
  • sound->getDefaults(&rate, 0, 0, 0);
  • sound->getLength (&lenbytes, FMOD_TIMEUNIT_PCMBYTES);
  • {
  • #if defined(WIN32) || defined(_WIN64) || defined(__WATCOMC__) || defined(_WIN32) || defined(__WIN32__)
  • #pragma pack(1)
  • #endif
  • /*
  • WAV Structures
  • */
  • typedef struct
  • {
  • signed char id[4];
  • int size;
  • } RiffChunk;
  • struct
  • {
  • RiffChunk chunk __PACKED;
  • unsigned short wFormatTag __PACKED; /* format type */
  • unsigned short nChannels __PACKED; /* number of channels (i.e. mono, stereo...) */
  • unsigned int nSamplesPerSec __PACKED; /* sample rate */
  • unsigned int nAvgBytesPerSec __PACKED; /* for buffer estimation */
  • unsigned short nBlockAlign __PACKED; /* block size of data */
  • unsigned short wBitsPerSample __PACKED; /* number of bits per sample of mono data */
  • } FmtChunk = { {{'f','m','t',' '}, sizeof(FmtChunk) - sizeof(RiffChunk) }, 1, channels, (int)rate, (int)rate * channels * bits / 8, 1 * channels * bits / 8, bits } __PACKED;
  • struct
  • {
  • RiffChunk chunk;
  • } DataChunk = { {{'d','a','t','a'}, lenbytes } };
  • struct
  • {
  • RiffChunk chunk;
  • signed char rifftype[4];
  • } WavHeader = { {{'R','I','F','F'}, sizeof(FmtChunk) + sizeof(RiffChunk) + lenbytes }, {'W','A','V','E'} };
  • #if defined(WIN32) || defined(_WIN64) || defined(__WATCOMC__) || defined(_WIN32) || defined(__WIN32__)
  • #pragma pack()
  • #endif
  • fp = fopen("record.wav", "wb");
  • /*
  • Write out the WAV header.
  • */
  • fwrite(&WavHeader, sizeof(WavHeader), 1, fp);
  • fwrite(&FmtChunk, sizeof(FmtChunk), 1, fp);
  • fwrite(&DataChunk, sizeof(DataChunk), 1, fp);
  • /*
  • Lock the sound to get access to the raw data.
  • */
  • sound->lock(0, lenbytes, &ptr1, &ptr2, &len1, &len2);
  • /*
  • Write it to disk.
  • */
  • fwrite(ptr1, len1, 1, fp);
  • /*
  • Unlock the sound to allow FMOD to use it again.
  • */
  • sound->unlock(ptr1, ptr2, len1, len2);
  • fclose(fp);
  • }
  • }
#include "recorder.h"
#include <cstdio>

#define __PACKED 

MyApp::MyApp()
{
}

bool MyApp::OnInit()
{
    MyFrame *frame = new MyFrame;

    frame->Show(true);
    return true;
}
IMPLEMENT_APP(MyApp)

BEGIN_EVENT_TABLE(MyFrame, wxFrame)

	EVT_CLOSE(MyFrame::OnClose)
	EVT_TIMER(wxID_ANY, MyFrame::OnTimer)
	EVT_BUTTON(wxID_ANY, MyFrame::OnClickStart)

END_EVENT_TABLE()


MyFrame::MyFrame() : wxFrame(   NULL, 
							    wxID_ANY, 
								_T("Spectrum"), 
								wxDefaultPosition, 
								wxSize(220, 100), 
								wxCAPTION      |
								wxTHICK_FRAME  |
								wxMINIMIZE_BOX |
								wxSTAY_ON_TOP  |
								wxSYSTEM_MENU  | 
								wxCLOSE_BOX    |
								wxFRAME_TOOL_WINDOW	)
{
	StartBtn = new wxButton(this, wxID_ANY, "START");
	InitSoundSystem();
	timer.SetOwner(this, wxID_ANY);
	timer.Start( 10 );
}


MyFrame::~MyFrame(void)
{

}


void MyFrame::InitSoundSystem(void)
{
	sound = NULL;
	system = NULL;

	result = FMOD::System_Create(&system);
	if (result != FMOD_OK)
	{
		wxLogError("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
		return;
	}

	result = system->setDriver(0);
	if(result != FMOD_OK)
	{
		wxLogError("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
		return;
	}

	result = system->setSoftwareChannels(100);
	if(result != FMOD_OK)
	{
		wxLogError("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
		return;
	}

	result = system->setHardwareChannels(32, 64, 32, 64);
	if( result != FMOD_OK )
	{
		wxLogError("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
		return;
	}

	result = system->setOutput(FMOD_OUTPUTTYPE_DSOUND);
	if (result != FMOD_OK)
	{
		wxLogError("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
		return;
	}

	result = system->init(200, FMOD_INIT_NORMAL, NULL);
	if (result != FMOD_OK)
	{
		wxLogError("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
		return;
	}
	result = system->setRecordDriver(0);
	if(result != FMOD_OK)
	{
		wxLogError("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
		return;
	}
    char name[256];

    result = system->getRecordDriverName(0, name, 256);

	wxLogDebug( name );

	memset(&exinfo, 0, sizeof(FMOD_CREATESOUNDEXINFO));

    exinfo.cbsize           = sizeof(FMOD_CREATESOUNDEXINFO);
    exinfo.numchannels      = 1;
    exinfo.format           = FMOD_SOUND_FORMAT_PCM16;
    exinfo.defaultfrequency = 44100;
    exinfo.length           = exinfo.defaultfrequency * sizeof(short) * exinfo.numchannels * 30;// 30 s
    
    result = system->createSound(0, FMOD_2D | FMOD_SOFTWARE | FMOD_OPENUSER, &exinfo, &sound);
	if (result != FMOD_OK)
	{
		wxLogError("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
		return;
	}
}

void MyFrame::OnClose(wxCloseEvent &WXUNUSED(event))
{
	system->recordStop();
	timer.Stop();

	SaveToWav( sound );

	Destroy();
}


void MyFrame::OnTimer(wxTimerEvent &WXUNUSED(event))
{
    unsigned int recordpos = 0;
    unsigned int length;

    sound->getLength(&length, FMOD_TIMEUNIT_PCM);
	system->getRecordPosition(&recordpos);

	wxString str;
	str.Printf("durée :%i s, pos: %i s", length, recordpos);

	wxLogDebug(str);
	SetTitle( str );
}

void MyFrame::OnClickStart(wxCommandEvent &WXUNUSED(event))
{
	result = system->recordStart(sound, false);
	if (result != FMOD_OK)
	{
		wxLogError("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
		return;
	}

	sound->setMode(FMOD_LOOP_NORMAL);
}

// Fonction empruntée a FMOD ( thanks )
void MyFrame::SaveToWav(FMOD::Sound *sound)
{
    FILE *fp;
    int             channels, bits;
    float           rate;
    void           *ptr1, *ptr2;
    unsigned int    lenbytes, len1, len2;

    if (!sound)
    {
        return;
    }

    sound->getFormat  (0, 0, &channels, &bits);
    sound->getDefaults(&rate, 0, 0, 0);
    sound->getLength  (&lenbytes, FMOD_TIMEUNIT_PCMBYTES);

    {
        #if defined(WIN32) || defined(_WIN64) || defined(__WATCOMC__) || defined(_WIN32) || defined(__WIN32__)
        #pragma pack(1)
        #endif
        
        /*
            WAV Structures
        */
        typedef struct
        {
	        signed char id[4];
	        int 		size;
        } RiffChunk;
    
        struct
        {
            RiffChunk       chunk           __PACKED;
            unsigned short	wFormatTag      __PACKED;    /* format type  */
            unsigned short	nChannels       __PACKED;    /* number of channels (i.e. mono, stereo...)  */
            unsigned int	nSamplesPerSec  __PACKED;    /* sample rate  */
            unsigned int	nAvgBytesPerSec __PACKED;    /* for buffer estimation  */
            unsigned short	nBlockAlign     __PACKED;    /* block size of data  */
            unsigned short	wBitsPerSample  __PACKED;    /* number of bits per sample of mono data */
        } FmtChunk  = { {{'f','m','t',' '}, sizeof(FmtChunk) - sizeof(RiffChunk) }, 1, channels, (int)rate, (int)rate * channels * bits / 8, 1 * channels * bits / 8, bits } __PACKED;

        struct
        {
            RiffChunk   chunk;
        } DataChunk = { {{'d','a','t','a'}, lenbytes } };

        struct
        {
            RiffChunk   chunk;
	        signed char rifftype[4];
        } WavHeader = { {{'R','I','F','F'}, sizeof(FmtChunk) + sizeof(RiffChunk) + lenbytes }, {'W','A','V','E'} };

        #if defined(WIN32) || defined(_WIN64) || defined(__WATCOMC__) || defined(_WIN32) || defined(__WIN32__)
        #pragma pack()
        #endif

        fp = fopen("record.wav", "wb");
       
        /*
            Write out the WAV header.
        */
        fwrite(&WavHeader, sizeof(WavHeader), 1, fp);
        fwrite(&FmtChunk, sizeof(FmtChunk), 1, fp);
        fwrite(&DataChunk, sizeof(DataChunk), 1, fp);

        /*
            Lock the sound to get access to the raw data.
        */
        sound->lock(0, lenbytes, &ptr1, &ptr2, &len1, &len2);

        /*
            Write it to disk.
        */
        fwrite(ptr1, len1, 1, fp);

        /*
            Unlock the sound to allow FMOD to use it again.
        */
        sound->unlock(ptr1, ptr2, len1, len2);

        fclose(fp);
    }
}

 Conclusion

-- rien a dire sinon, qu'il faut appuyer sur le bouton pour capturer

 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip


 Sources du même auteur

Source avec Zip Source avec une capture [ FMOD | WXWIDGETS]RENDU SPECTRUM -- MP3
Source avec Zip Source avec une capture WXWIDGETS -- > CLIENT FTP
Source avec Zip Source avec une capture ~~ PETIT LECTEUR VIDEO [ WXWIDGETS ----> WXMEDIACTRL ] ~~
Source avec Zip Source avec une capture --LECTEUR MP3/CD [WXWIDGETS+FMOD]--

 Sources de la même categorie

Source avec Zip ADAPTER LES TEMPS DE SUBTITLES DE SOUS TITRAGE ENTRE DEUX LA... par berrami
Source avec Zip Source avec une capture DÉTECTION DE VISAGE (YEUX, NEZ, BOUCHE) AVEC OPENCV EN TEMPS... par MadM@tt
Source avec Zip Source avec une capture GÉNÉRATEUR FM EXPÉRIMENTAL par tontonCD
Source avec Zip LECTEUR MULTIMÉDIA par omegatou
Source avec Zip IMPLÉMENTATION D'UN ALGORITHME DE COMPRESSION/DECOMPRESSION ... par eemikhm

 Sources en rapport avec celle ci

WXFREECHART PIEPLOT EXAMPLE par neria
Source avec Zip KMEANS OPENCV WXWIDGETS par walidoss1
Source avec Zip Source avec une capture CAPTURE ECRAN OU WEBCAM par TheTiger
Source avec Zip CAPTURE ECRAN par f_l_a_s_h_b_a_c_k
Source avec Zip Source avec une capture HMEDIAV2 LECTEUR (WIN32) par hattabking

Commentaires et avis

Commentaire de satellite34 le 11/04/2006 19:17:36

voila, tout simplement, ceci est une abstraction de waveOut, bien plus facile a utiliser pour les novices.....

Commentaire de electrogeek21 le 18/03/2010 16:05:46

Bonjour, votre programme est très interessant mais malheureusement quand j'essaie d'couter le son enregistré, ya rien ...

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

Besion d'aide pour Capture Video WDM en C++ [ par kodoma ] Je dois faire un programme en c++, ki permette de capturer une video à l'aide des drivers WDM et de pouvoir récupérer une seule image ki sera sauvegar capture d' écran ! [ par vdox05 ] Bonjour,Je voudrais savoir comment on fait pour faire une capture d' écran depuis un programme et l' enregistrer dans un fichier .Merci ;-) capture d'ecran [ par obasileus ] Bonjour a tous,savez vous comment faire une capture d'ecran en c/c++ ?Thxobasileus Capture d'écran [ par joyeuxlutin ] Bonjour Sur mon application, j'ai un controle (zone de texte à plusieurs lignes). Je peux faire la sauvegarde de ce controle en fichier *.txt.Je désir Capture d'écran [ par joyeuxlutin ] Bonjour Sur mon application, j'ai un controle (zone de texte à plusieurs lignes). Je peux faire la sauvegarde de ce controle en fichier *.txt.Je désir Capture du clavier [ par garslouche ] Bonjour,j'aimerais savoir comment on fait pour capturer un événement du clavier quand on n'a pas le focus.En gros je voudrais pouvoir créer mes propre Capture d'événement fenêtre sous windows [ par annplop ] Bonjour,le réalise une application pilotant une autre application cette dernière envoie des fenêtres que je souhaiterais capturer, ensuite je souhaite DC to bmp [ par sebseb42 ] salut a tous,voila, j'utilise la fonction BitBlt pour faire une capture d'ecran, ca c'est cool, ca fonctionne, j'ai la capture dans un DC, mnt ce que Capture Net Send [ par coluche ] Bonjour a tous,j'aimerai savoir si en c++ il serai possible de capturer le texte de net send entrant et le mettre dans une variable CString.si quelqu' Capture d'écran et envoi de mail [ par jbHTS ] Bonjour tout le monde,je souhaiterai savoir si il existe une fonction permettant de faire une capture d'écran et si vous connaissez des cours en franç


Nos sponsors


Sondage...

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

Photothèque

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,437 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales