extras; samples: Fixed warnings
[sdk] / samples / audio / DirectBufferPlayer / wavPlayer.ec
1 import "EcereAudio"
2
3 class Form1 : Window
4 {
5    caption = "Form1";
6    background = activeBorder;
7    borderStyle = sizable;
8    hasMaximize = true;
9    hasMinimize = true;
10    hasClose = true;
11    size = { 640, 480 };
12
13    Sound sound { };
14    int pos;
15
16    Button button1
17    {
18       this, caption = "Play", position = { 200, 168 };
19
20       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
21       {
22          pos = 0;
23          PauseAudio(0);
24          return true;
25       }
26    };
27
28    bool OnPostCreate()
29    {
30       if(sound.Load("sweep.wav"))
31       {
32          AudioSpec wantedSpec
33          {
34             freq = sound.frequency; //44100;
35             bits = sound.bits; //16;
36             channels = sound.channels; //2;
37             samples = AUDIO_BUFFER_SIZE;
38             callback = AudioCallback;
39             userdata = this;
40             windowHandle = systemHandle;
41             volume = 100;
42          };
43          AudioSpec spec { };
44
45          if(OpenAudio(wantedSpec, spec) < 0)
46          {
47             MessageBox { contents = "OpenAudio epic fail" }.Modal();
48             return false;
49          }
50          return true;
51       }
52       else
53          MessageBox { contents = "sweep.wav not found" }.Modal();
54       return false;
55    }
56
57    void OnDestroy()
58    {
59       CloseAudio();
60    }
61
62    void AudioCallback(byte *stream, int len)
63    {
64       static byte buffer[AUDIO_BUFFER_SIZE];
65       int s = Min(sound.length * (sound.bits == 16 ? 2 : 1) - pos, len);
66       memcpy(buffer, sound.data + pos, s);
67       pos += s;
68       if(s < len)
69       {
70          if(sound.bits == 8)
71          {
72             /*if(s)
73             {
74                byte value = sound.data[pos-1];
75                int c;
76                for(c = s; c < len; c++)
77                {
78                   if(value < 128) value++; else if(value > 128) value--; else break;
79                   buffer[c] = value;
80                }
81                s = c;
82             }*/
83             memset(buffer + s, 128, len - s);
84          }
85          else
86             memset(buffer + s, 0, len - s);
87       }
88       memcpy(stream, buffer, len);
89    }
90 }
91
92 Form1 form1 { };