summaryrefslogtreecommitdiff
path: root/audio/Acx/Samples/Common/WaveReader.h
blob: 08516be7944832a0a6394658909241252ab76496 (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*++

Copyright (c) Microsoft Corporation.  All rights reserved.

    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
    KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
    PURPOSE.

Module Name:

    WaveReader.h

Abstract:

    Declaration of wave reader for ACX sample drivers.


--*/
#pragma once

#define _USE_MATH_DEFINES
#include <math.h>
#include <limits.h>

#define NUM_OF_CHUNK_FOR_FILE_READ 2

class CWaveReader;

// Wave header structure decleration 
typedef CWaveReader *pWaveReader;
typedef struct _WAVEHEADER
{
    BYTE chunkId[4];
    ULONG chunkSize;
    BYTE format[4];
    BYTE subChunkId[4];
    ULONG subChunkSize;
    WORD audioFormat;
    WORD numChannels;
    ULONG sampleRate;
    ULONG bytesPerSecond;
    WORD blockAlign;
    WORD bitsPerSample;
    BYTE dataChunkId[4];
    ULONG dataSize;
}WAVEHEADER;

typedef struct _CHUNKDESCRIPTOR
{
    PBYTE pStartAddress;        // Starting address of the chunk
    ULONG ulChunkLength;        // Length of the chunk
    bool  bIsChunkEmpty;        // If the chunk is empty
}CHUNKDESCRIPTOR;
typedef CHUNKDESCRIPTOR *PCHUNKDESCRIPTOR;

/*
    The idea here is to allocate one second long worth of buffer and divide it into NUM_OF_CHUNK_FOR_FILE_READ chunks.
    In one file read operation we read and fill one chunk data . The chunk will be emptied every 10 ms by OS.
    Once the OS empties one chunk data we schedule a workitem to read and fill next available chunk. 
*/

typedef struct _WAVEDATAQUEUE
{
    PBYTE pWavData;     // Pointer to the temporary buffer for reading one second worth of data from wave file
    ULONG ulLength;     // length of pWavData in bytes
    ULONG ulReadPtr;    // current reading position in pWavData in bytes
    bool  bEofReached;  // This will be set once the eof is reached.
    WORD  currentExecutedChunk;
    CHUNKDESCRIPTOR sChunkDescriptor[NUM_OF_CHUNK_FOR_FILE_READ];
}WAVEDATAQUEUE;
typedef WAVEDATAQUEUE *PWAVEDATAQUEUE;

// Parameter to workitem.
#include <pshpack1.h>
typedef struct _READWORKER_PARAM {
    PIO_WORKITEM     WorkItem;      // Pointer to the workitem
    KEVENT           EventDone;     // Used for synchronizing a workitem for scheduling. 
    pWaveReader      PtrWaveReader;   // pointer to the wavereader class.
    PCHUNKDESCRIPTOR PtrChunkDescriptor;   // chunk descriptor for the chunk, which needs to be filled after file read
} READWORKER_PARAM;
typedef READWORKER_PARAM *PREADWORKER_PARAM;
#include <poppack.h>

__drv_maxIRQL(PASSIVE_LEVEL)
PAGED_CODE_SEG
IO_WORKITEM_ROUTINE ReadFrameWorkerCallback;

// Wave Reader class

class CWaveReader
{

public:
    HANDLE                      m_FileHandle;       // Wave File handle.
    WORD                        m_ChannelCount;     // Number of Channels for the stream during stream init
    WORD                        m_BitsPerSample;    // Number of Bits per sample for the stream during stream init
    DWORD                       m_SamplesPerSecond; // Number of Sample per second for the stream during stream init
    bool                        m_Mute;             // Capture Zero buffer if mute
    OBJECT_ATTRIBUTES           m_objectAttributes; // Used for opening file.
    WAVEDATAQUEUE               m_WaveDataQueue;    // Big buffer data object and its current state
    KMUTEX                      m_FileSync;         // Synchronizes file access
    WAVEHEADER                  m_WaveHeader;

public:
    __drv_maxIRQL(PASSIVE_LEVEL)
    PAGED_CODE_SEG
    CWaveReader();

    __drv_maxIRQL(PASSIVE_LEVEL)
    PAGED_CODE_SEG
    ~CWaveReader();

    __drv_maxIRQL(PASSIVE_LEVEL)
    PAGED_CODE_SEG
    NTSTATUS Init
    (
        _In_ PWAVEFORMATEXTENSIBLE WfExt,
        _In_ PUNICODE_STRING puiFileName
    );

    __drv_maxIRQL(DISPATCH_LEVEL)
    VOID ReadWaveData
    (
        _Out_writes_bytes_(BufferLength) BYTE  *Buffer,
        _In_ ULONG      BufferLength
    );

    __drv_maxIRQL(PASSIVE_LEVEL)
    PAGED_CODE_SEG
    VOID SetMute(_In_ bool Value)
    {
        PAGED_CODE();

        m_Mute = Value;
    }

    __drv_maxIRQL(PASSIVE_LEVEL)
    PAGED_CODE_SEG
    void WaitAllWorkItems();

    // Static allocation totally related to the workitems for reading data from wavefile and putting it to chunk buffer
    static PDEVICE_OBJECT       m_pDeviceObject;
    static PREADWORKER_PARAM    m_pWorkItems;
    PAGED_CODE_SEG
    static NTSTATUS  InitializeWorkItems(_In_ PDEVICE_OBJECT  DeviceObject);

    __drv_maxIRQL(DISPATCH_LEVEL)
    static PREADWORKER_PARAM GetNewWorkItem();

    __drv_maxIRQL(PASSIVE_LEVEL)
    PAGED_CODE_SEG
    static VOID DestroyWorkItems();

private:
    __drv_maxIRQL(DISPATCH_LEVEL)
    VOID ReadWavChunk(PCHUNKDESCRIPTOR PtrChunkDescriptor);

    __drv_maxIRQL(DISPATCH_LEVEL)
    bool IsAllChunkEmpty();

    __drv_maxIRQL(PASSIVE_LEVEL)
    PAGED_CODE_SEG
    NTSTATUS OpenWaveFile(PUNICODE_STRING puiFileName);

    __drv_maxIRQL(PASSIVE_LEVEL)
    PAGED_CODE_SEG
    NTSTATUS FileClose();

    __drv_maxIRQL(PASSIVE_LEVEL)
    PAGED_CODE_SEG
    NTSTATUS FileReadHeader();

    __drv_maxIRQL(PASSIVE_LEVEL)
    PAGED_CODE_SEG
    NTSTATUS FileOpen();

    __drv_maxIRQL(DISPATCH_LEVEL)
    VOID CopyDataFromRingBuffer
    (
        _Out_writes_bytes_(BufferLength) BYTE       *Buffer,
        _In_                             ULONG      BufferLength
    );

    __drv_maxIRQL(PASSIVE_LEVEL)
    PAGED_CODE_SEG
    NTSTATUS AllocateBigBuffer();

    __drv_maxIRQL(PASSIVE_LEVEL)
    PAGED_CODE_SEG
    NTSTATUS ReadHeaderAndFillBuffer();

    friend IO_WORKITEM_ROUTINE ReadFrameWorkerCallback;
};