File: stream.c

package info (click to toggle)
directfb 1.7.7-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,212 kB
  • sloc: ansic: 306,760; cpp: 46,357; sh: 11,720; makefile: 5,620; perl: 662; asm: 507; xml: 116
file content (93 lines) | stat: -rw-r--r-- 2,565 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>

#include <fusionsound.h>

static void
feed_stream (IFusionSoundStream *stream)
{
     DirectResult ret;
     int       i;
     s16       buf[16384];

     /* Generate woops ;) */
     for (i=0; i<16384; i++)
          buf[i] = (s16)( sin(i*i/(16384.0*16384.0)*M_PI*200) * 10000 );

     /* Write eight times (~3 seconds of playback). */
     for (i=0; i<8; i++) {
          /*
           * Wait for a larger chunk of free space. Avoids a blocking write
           * with very small partially writes each time there's new space.
           *
           * This wait is for demonstrational purpose, in practice blocking
           * writes are no overhead and always keeping the buffer at the
           * highest fill level is more safe.
           */
          ret = stream->Wait (stream, 16384);
          if (ret) {
               FusionSoundError ("IFusionSoundStream::Write", ret);
               return;
          }

          /* This write won't block anymore. */
          ret = stream->Write (stream, buf, 16384);
          if (ret) {
               FusionSoundError ("IFusionSoundStream::Write", ret);
               return;
          }
     }
}

int
main (int argc, char *argv[])
{
     DirectResult         ret;
     IFusionSound        *sound;
     IFusionSoundStream  *stream;
     FSStreamDescription  desc;

     ret = FusionSoundInit (&argc, &argv);
     if (ret)
          FusionSoundErrorFatal ("FusionSoundInit", ret);

     /* Retrieve the main sound interface. */
     ret = FusionSoundCreate (&sound);
     if (ret)
          FusionSoundErrorFatal ("FusionSoundCreate", ret);

     /* Fill stream description. */
     desc.flags        = FSSDF_SAMPLERATE | FSSDF_BUFFERSIZE |
                         FSSDF_CHANNELS   | FSSDF_SAMPLEFORMAT;
     desc.samplerate   = 44100;
     desc.buffersize   = 32768;
     desc.channels     = 1;
     desc.sampleformat = FSSF_S16;

     /* Create the sound stream and feed it. */
     ret = sound->CreateStream (sound, &desc, &stream);
     if (ret) {
          FusionSoundError ("IFusionSound::CreateStream", ret);
     }
     else {
          /* Fill the ring buffer with our generated data. */
          feed_stream (stream);

          /* Wait for end of stream (ring buffer holds ~3/4 sec). */
          stream->Wait (stream, 0);

          stream->Release (stream);
     }

     sound->Release (sound);

     return 0;
}