Jump to content
Sign in to follow this  
alpin19

Prosty Player Na Alsie (c++)

Recommended Posts

Witam

 

Na podstawie alsowego tutoriala napisałem prostego player'a:

#include <stdio.h>
    #include <stdlib.h>
    #include <alsa/asoundlib.h>
          
    main (int argc, char *argv[])
    {
        int i;
        int err;
        short buf[128];
        snd_pcm_t *playback_handle;
        snd_pcm_hw_params_t *hw_params;
    
        if ((err = snd_pcm_open (&playback_handle, argv[1], SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
            fprintf (stderr, "cannot open audio device %s (%s)\n", 
                 argv[1],
                 snd_strerror (err));
            exit (1);
        }
           
        if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
            fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
                 snd_strerror (err));
            exit (1);
        }
                 
        if ((err = snd_pcm_hw_params_any (playback_handle, hw_params)) < 0) {
            fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
                 snd_strerror (err));
            exit (1);
        }
    
        if ((err = snd_pcm_hw_params_set_access (playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
            fprintf (stderr, "cannot set access type (%s)\n",
                 snd_strerror (err));
            exit (1);
        }
    
        if ((err = snd_pcm_hw_params_set_format (playback_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
            fprintf (stderr, "cannot set sample format (%s)\n",
                 snd_strerror (err));
            exit (1);
        }
    
        if ((err = snd_pcm_hw_params_set_rate_near (playback_handle, hw_params, 44100, 0)) < 0) {
            fprintf (stderr, "cannot set sample rate (%s)\n",
                 snd_strerror (err));
            exit (1);
        }
    
        if ((err = snd_pcm_hw_params_set_channels (playback_handle, hw_params, 2)) < 0) {
            fprintf (stderr, "cannot set channel count (%s)\n",
                 snd_strerror (err));
            exit (1);
        }
    
        if ((err = snd_pcm_hw_params (playback_handle, hw_params)) < 0) {
            fprintf (stderr, "cannot set parameters (%s)\n",
                 snd_strerror (err));
            exit (1);
        }
    
        snd_pcm_hw_params_free (hw_params);
    
        if ((err = snd_pcm_prepare (playback_handle)) < 0) {
            fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
                 snd_strerror (err));
            exit (1);
        }
    
        for (i = 0; i < 10; ++i) {
            if ((err = snd_pcm_writei (playback_handle, buf, 128)) != 128) {
                fprintf (stderr, "write to audio interface failed (%s)\n",
                     snd_strerror (err));
                exit (1);
            }
        }
    
        snd_pcm_close (playback_handle);
        exit (0);
    }

 

Kiedy próbuję go kompilować dostaję taki wynik:

 

[root@zanegi mpeeg]# g++ player.cpp -o player
player.cpp:69:2: warning: no newline at end of file
player.cpp: In function ‘int main(int, char**)’:
player.cpp:13: error: expected initializer before ‘if’
player.cpp:19: error: ‘hw_params’ was not declared in this scope
player.cpp:24: error: ‘hw_params’ was not declared in this scope
player.cpp:29: error: ‘hw_params’ was not declared in this scope
player.cpp:32: error: expected `;' before ‘exit’
player.cpp:34: error: ‘hw_params’ was not declared in this scope
player.cpp:39: error: ‘hw_params’ was not declared in this scope
player.cpp:44: error: ‘hw_params’ was not declared in this scope
player.cpp:50: error: ‘hw_params’ was not declared in this scope
player.cpp:66: error: expected `;' before numeric constant
player.cpp:66: warning: statement is a reference, not call, to function ‘exit’

 

:(

 

Share this post


Link to post
Share on other sites

Popraw błędy w tych linijkach co ci kompilator zapodał, a poza tym to masz warna za tak długi listing. Używa się do tak długich listingów Codeboxa :)

Share this post


Link to post
Share on other sites

Musisz dołączyć odpowiednią bibliotekę przy kompilacji. Zapodaj linka do tego kursu, tam powinno być. Do tego zmień main na int main, w linii 44 zamień 44100 na (unsigned int*)44100.

Share this post


Link to post
Share on other sites
Guest amdfanatyk

w linii 44 zamień 44100 na (unsigned int*)44100.

 

co to by mialo byc? z tego co wiem nie ma zmiennych o nazwach liczbowych, zatem dlaczego 44100 chcesz rzutowac na wskazanie na uint? chyba, ze 44100 to adres jest.

Share this post


Link to post
Share on other sites

invalid conversion from int to unsigned int*

 

Pluje się przy 3-cim argumencie funkcji snd_pcm_hw_params_set_rate_near()

Share this post


Link to post
Share on other sites
Guest amdfanatyk

ludzie wezcie sobie jakis program najpierw napiszcie bo takie pierdoly jak tu wypisujecie to sie plakac chce i rece opadaja, jakies podstawy c/c++ by sie przydaly, wiedziec co to wskazanie, referencja;

 

unsigned int var = 1234;
unsigned int * pVar = &var;

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...