Accueil > > > [ FMODEX | WXWIDGETS ] CAPTURER L'OUTPUT AUDIO + ÉCRITURE DANS UN WAV
[ FMODEX | WXWIDGETS ] CAPTURER L'OUTPUT AUDIO + ÉCRITURE DANS UN WAV
Information sur la source
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
Sources du même auteur
Sources de la même categorie
Commentaires et avis
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ç
|
Derniers Blogs
[FRAMEWORK 4] LES TASKS ET LE THREAD UI[FRAMEWORK 4] LES TASKS ET LE THREAD UI par fathi
Je viens de passer quelques temps au TechDay's et j'ai pu voir pas mal de session intéressante. Par contre une chose m'a un peu étonné lors de certaines de ces sessions qui abordaient les améliorations du framework .NET (donc le 4.5) : en gros, bea...
Cliquez pour lire la suite de l'article par fathi WORKFLOW FOUNDATION 3 A UN PIED DANS LA TOMBEWORKFLOW FOUNDATION 3 A UN PIED DANS LA TOMBE par JeremyJeanson
Depuis déjà un an, je conseille vivement les utilisateurs de Workflow Foundation 3 à migrer vers la version 4. L'information qui va suivre ne devrait donc pas trop prendre au dépourvu les personnes qui m'ont suivi. Je profite de ce poste, pour faire le re...
Cliquez pour lire la suite de l'article par JeremyJeanson TECHDAYS PARIS 2012 : NOUVELLES TENDANCES DU POSTE DE TRAVAIL - BRING YOUR OWN PCTECHDAYS PARIS 2012 : NOUVELLES TENDANCES DU POSTE DE TRAVAIL - BRING YOUR OWN PC par ROMELARD Fabrice
Speakers: Thierry Rapatout, Antoine Petit et Xavier Trebbia Cette session entre dans le cadre des RDV Décideurs des TechDays 2012, elle est liée à la consumérisation de l'IT et la mise en place du "DeskTop as a Service" dans de plus en ...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : SYSTEM CENTER SERVICE MANAGER 2012 VUE D'ENSEMBLETECHDAYS PARIS 2012 : SYSTEM CENTER SERVICE MANAGER 2012 VUE D'ENSEMBLE par ROMELARD Fabrice
Speakers: Julien Marechal, Gautier Confiant, Sébastien MEYER La session débute par le positionnement de la solution System Center par rapport aux concepts d'organisation ITIL. Le portail du catalogue de se...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2012 : PLEINIèRE SECOND JOURTECHDAYS PARIS 2012 : PLEINIèRE SECOND JOUR par ROMELARD Fabrice
Après une première journée dédiée aux développeurs, cette seconde journée est dédiée au monde des entreprises et de ses applications. Ainsi, cette pleinière est dédiée à faire un 360 de l'évolution des applications Business aux demandes ac...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|