OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Native Client Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef WEB_WAV_SOUND_RESOURCE_H_ | |
6 #define WEB_WAV_SOUND_RESOURCE_H_ | |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "experimental/conways_life/audio/audio_source.h" | |
12 #include "experimental/conways_life/web_resource_loader.h" | |
13 | |
14 namespace pp { | |
15 class Instance; | |
16 } | |
17 | |
18 namespace audio { | |
19 | |
20 // WebWavSoundResource downloads a wav audio file from a URL and, if successful, | |
21 // exposes the sound samples through the AudioSource interface. Each instance | |
22 // can only be used once. That is, once Init has been called there is no way | |
23 // to call it again to load a different sound resource. As such, this class | |
24 // uses a lock-free, producer-consumer approach to thread safety. It starts | |
25 // with IsReady returning false. Once the audio data is available, IsReady | |
26 // reports true and remains so until the instance is deleted. | |
27 class WebWavSoundResource : public AudioSource { | |
28 public: | |
29 typedef life::WebResourceLoader<WebWavSoundResource> Loader; | |
30 | |
31 WebWavSoundResource(); | |
32 ~WebWavSoundResource(); | |
33 | |
34 // Init the wav sound source by downloading audio data from the given URL. | |
35 // This is an asychronous call that returns immediately. | |
36 void Init(const std::string& url, pp::Instance* instance); | |
37 | |
38 // Return whether the audio sample is ready to play. | |
39 bool IsReady() const { return is_ready_; } | |
40 | |
41 // AudioSource interface. | |
42 int32_t GetSampleRate() const; | |
43 const char* GetAudioData() const; | |
44 size_t GetAudioDataSize() const; | |
45 | |
46 // WebResourceLoader delegate methods. | |
47 bool OnWebResourceLoaderCallback(Loader::DispatchOpCode op_code, | |
48 Loader* loader); | |
49 void OnWebResourceLoaderError(int32_t error, Loader* loader); | |
50 void OnWebResourceLoaderDone(Loader* loader); | |
51 | |
52 private: | |
53 // Disallow copy and assigment. | |
54 WebWavSoundResource(const WebWavSoundResource&); | |
55 WebWavSoundResource& operator=(const WebWavSoundResource&); | |
56 | |
57 // Clear (delete) all audio data. | |
58 void Clear(); | |
59 | |
60 // True ==> wav data is ready for audio playback. | |
61 bool is_ready_; | |
62 // Wav audio data, including headers. | |
63 std::vector<char> wav_data_; | |
64 size_t wav_data_size_; | |
65 // The actual amount of data received from the loader. | |
66 size_t received_data_size_; | |
67 // The resource loader used to stream data. | |
68 Loader* loader_; | |
69 }; | |
70 | |
71 } // namespace audio | |
72 | |
73 #endif // WEB_WAV_SOUND_RESOURCE_H_ | |
OLD | NEW |