OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
OcAudioGenBeep.c
Go to the documentation of this file.
1
15#include <Library/BaseMemoryLib.h>
16#include <Library/BaseOverflowLib.h>
17#include <Library/DebugLib.h>
18#include <Library/MemoryAllocationLib.h>
19#include <Library/OcAudioLib.h>
20#include <Library/OcMiscLib.h>
21#include <Library/UefiBootServicesTableLib.h>
22
23#include <Protocol/AppleHda.h>
26
27#include "OcAudioInternal.h"
28
29#define OC_BEEP_AUDIO_FREQUENCY EfiAudioIoFreq44kHz
30#define OC_BEEP_AUDIO_BITS EfiAudioIoBits16
31#define OC_BEEP_AUDIO_CHANNELS 2
32#define OC_BEEP_AUDIO_LENGTH 3810
33
34//
35// TODO: This is a sine curve, need to properly approximate it.
36// FIXME: Mono audio lags like hell with AudioDxe.
37//
38STATIC INT16 mAudioBeepSignal[] = {
39 -1504, -1504, -5272, -5272, -10116, -10116, -15172, -15172, -19368, -19368, -22302, -22302, -23904, -23904, -24295, -24295, -23519, -23519, -21508, -21508, -17967, -17967, -13030, -13030, -7321, -7321, -1404, -1404, 4370, 4370, 10050, 10050,
40 15890, 15890, 21430, 21430, 25688, 25688, 27981, 27981, 28493, 28493, 28201, 28201, 27670, 27670, 26490, 26490, 24152, 24152, 20977, 20977, 17602, 17602, 14654, 14654, 12061, 12061, 9200, 9200, 5857, 5857, 2474, 2474, -527, -527, -3031, -3031,
41 -5103, -5103, -6973, -6973, -8623, -8623, -9690, -9690, -10039, -10039, -9923, -9923, -9535, -9535, -9031, -9031, -8553, -8553, -7876, -7876, -6875, -6875, -5608, -5608, -4026, -4026, -2370, -2370, -1121, -1121, -336, -336, 131, 131, 367, 367,
42 257, 257, -396, -396, -1404, -1404, -2396, -2396, -3258, -3258, -3866, -3866, -4198, -4198, -4613, -4613, -5018, -5018, -5082, -5082, -4812, -4812, -4197, -4197, -3242, -3242, -2204, -2204, -1307, -1307, -473, -473, 470, 470, 1514, 1514, 2245, 2245,
43 2218, 2218, 1336, 1336, -32, -32, -971, -971, -1076, -1076, -936, -936, -1275, -1275, -2143, -2143, -3144, -3144, -3688, -3688, -3565, -3565, -3090, -3090, -2382, -2382, -1042, -1042, 1532, 1532, 5614, 5614, 10709, 10709, 15757, 15757,19722, 19722,
44 22252, 22252, 23435, 23435, 23553, 23553, 22752, 22752, 20692, 20692, 17020, 17020, 12007, 12007, 6316, 6316, 570, 570, -4909, -4909, -10447, -10447, -16295, -16295, -21831, -21831, -25958, -25958, -27987, -27987, -28310, -28310,-28113, -28113,
45 -27797, -27797, -26762, -26762, -24515, -24515, -21306, -21306, -17897, -17897, -14966, -14966, -12300, -12300, -9235, -9235, -5690, -5690, -2042, -2042, 1325, 1325, 4035, 4035, 6121, 6121, 7910, 7910, 9433, 9433, 10360, 10360, 10655, 10655,
46 10410, 10410, 9832, 9832, 9220, 9220, 8653, 8653, 7880, 7880, 6845, 6845, 5421, 5421, 3623, 3623, 1968, 1968, 898, 898, 333, 333, 19, 19, -146, -146, 97, 97, 919, 919, 1945, 1945, 2814, 2814, 3448, 3448, 3795, 3795, 4155, 4155, 4605, 4605,
47 4751, 4751, 4500, 4500, 3993, 3993, 3218, 3218, 2196, 2196, 1248, 1248, 385, 385, -354, -354, -1161, -1161, -1758, -1758, -1857, -1857, -1105, -1105, 254, 254, 1642, 1642, 2095, 2095, 1912, 1912, 1827, 1827, 2309, 2309, 2993, 2993, 3416, 3416,
48 3203, 3203, 2549, 2549, 1911, 1911
49};
50
51EFI_STATUS
52EFIAPI
54 IN UINT32 ToneCount,
55 IN UINTN ToneLength,
56 IN UINTN SilenceLength
57 )
58{
59 EFI_STATUS Status;
60 OC_AUDIO_PROTOCOL *OcAudio;
62 UINTN ToneSlots;
63 UINTN Index;
64 UINTN BufferSize;
65 UINT8 *Buffer;
66 UINT8 *BufferPtr;
67
68 DEBUG ((DEBUG_INFO, "OCAU: Beep %u %u/%u\n", ToneCount, (UINT32)ToneLength, (UINT32)SilenceLength));
69
70 if ((ToneCount == 0) || ((ToneLength == 0) && (SilenceLength == 0))) {
71 return EFI_SUCCESS;
72 }
73
74 Status = gBS->LocateProtocol (
76 NULL,
77 (VOID **)&OcAudio
78 );
79 if (EFI_ERROR (Status)) {
80 DEBUG ((DEBUG_INFO, "OCAU: Beep has missing OcAudio protocol - %r\n", Status));
81 return EFI_ABORTED;
82 }
83
85 if (Private->AudioIo == NULL) {
86 DEBUG ((DEBUG_INFO, "OCAU: Beep has AudioIo not configured\n"));
87 return EFI_ABORTED;
88 }
89
90 //
91 // Convert to microseconds.
92 //
93 if ( BaseOverflowMulUN (ToneLength, 1000, &ToneLength)
94 || BaseOverflowMulUN (SilenceLength, 1000, &SilenceLength))
95 {
96 return EFI_INVALID_PARAMETER;
97 }
98
99 //
100 // Wait for enough microseconds if no signals are given.
101 //
102 if (ToneLength == 0) {
103 for (Index = 0; Index < ToneCount; ++Index) {
104 gBS->Stall (SilenceLength);
105 }
106
107 return EFI_SUCCESS;
108 }
109
110 //
111 // Rough signal time calculation, not sure whether we need to overcomplicate things right now.
112 //
113 ToneSlots = MAX (ToneLength / OC_BEEP_AUDIO_LENGTH, 1);
114 if (BaseOverflowMulUN (ToneSlots, sizeof (mAudioBeepSignal), &BufferSize)) {
115 return EFI_INVALID_PARAMETER;
116 }
117
118 //
119 // TODO: Remove this piece of junk and fix SetupPlayback performance.
120 //
121 Buffer = AllocatePool (BufferSize);
122 if (Buffer == NULL) {
123 return EFI_OUT_OF_RESOURCES;
124 }
125
126 BufferPtr = Buffer;
127 for (Index = 0; Index < ToneSlots; ++Index) {
128 CopyMem (BufferPtr, &mAudioBeepSignal[0], sizeof (mAudioBeepSignal));
129 BufferPtr += sizeof (mAudioBeepSignal);
130 }
131
132 Private->OcAudio.StopPlayback (&Private->OcAudio, TRUE);
133
134 Status = Private->AudioIo->SetupPlayback (
135 Private->AudioIo,
136 Private->OutputIndexMask,
137 Private->Gain,
141 Private->PlaybackDelay
142 );
143 if (EFI_ERROR (Status)) {
144 DEBUG ((DEBUG_INFO, "OCAU: Beep playback setup failure - %r\n", Status));
145 FreePool (Buffer);
146 return EFI_ABORTED;
147 }
148
149 for (Index = 0; Index < ToneCount; ++Index) {
150 Status = Private->AudioIo->StartPlayback (
151 Private->AudioIo,
152 Buffer,
153 BufferSize,
154 0
155 );
156 if (EFI_ERROR (Status)) {
157 DEBUG ((DEBUG_INFO, "OCAU: Beep playback failure - %r\n", Status));
158 break;
159 }
160
161 if (SilenceLength > 0) {
162 gBS->Stall (SilenceLength);
163 }
164 }
165
166 FreePool (Buffer);
167
168 return Status;
169}
EFI_GUID gOcAudioProtocolGuid
STATIC INT16 mAudioBeepSignal[]
#define OC_BEEP_AUDIO_BITS
EFI_STATUS EFIAPI InternalOcAudioGenBeep(IN UINT32 ToneCount, IN UINTN ToneLength, IN UINTN SilenceLength)
#define OC_BEEP_AUDIO_FREQUENCY
#define OC_BEEP_AUDIO_CHANNELS
#define OC_BEEP_AUDIO_LENGTH
Microseconds.
#define OC_AUDIO_PROTOCOL_PRIVATE_FROM_OC_AUDIO(Proto)
EFI_BOOT_SERVICES * gBS
OC_TYPING_BUFFER_ENTRY Buffer[OC_TYPING_BUFFER_SIZE]
Definition OcTypingLib.h:42
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
#define MAX(a, b)
Definition coder.h:59
EFI_AUDIO_IO_START_PLAYBACK StartPlayback
Definition AudioIo.h:283
EFI_AUDIO_IO_SETUP_PLAYBACK SetupPlayback
Definition AudioIo.h:282
OC_AUDIO_STOP_PLAYBACK StopPlayback
Definition OcAudio.h:263
OC_AUDIO_PROTOCOL OcAudio
EFI_AUDIO_IO_PROTOCOL * AudioIo