-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGenerateAudioOutput.s.sol
More file actions
61 lines (50 loc) · 2.5 KB
/
Copy pathGenerateAudioOutput.s.sol
File metadata and controls
61 lines (50 loc) · 2.5 KB
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;
import {Script} from "forge-std/Script.sol";
import {console} from "forge-std/Test.sol";
import {Base64} from "solady/utils/Base64.sol";
import {DynamicBufferLib} from "solady/utils/DynamicBufferLib.sol";
import {LibString} from "solady/utils/LibString.sol";
import {FixedPointMathLib as Math} from "solady/utils/FixedPointMathLib.sol";
import {FiveFiveFiveAudio} from "src/utils/FiveFiveFiveAudio.sol";
/// @notice A script to create and write the WAV audio file of the arrangement
/// of “Gonna Fly Now” by Bill Conti completely with smart contracts.
/// @dev You must run this script with a high `--memory-limit` option (e.g.
/// `50_000_000_000` works) and `--via-ir`.
contract GenerateAudioOutputScript is Script {
using DynamicBufferLib for DynamicBufferLib.DynamicBuffer;
using LibString for uint256;
using Math for int256;
using Math for uint256;
// -------------------------------------------------------------------------
// Constants
// -------------------------------------------------------------------------
/// @notice The total number of ticks for 1 cycle of the audio.
/// @dev Note that the length of the WAV file is written into the header
/// returned by {FiveFiveFiveAudio.getAudioWavFileHeader} to take in a
/// maximum of `776*2**10` samples. To change this, update the header
/// returned accordingly.
uint256 internal constant TICKS_PER_CYCLE = 776 << 10;
// -------------------------------------------------------------------------
// Script `run()`
// -------------------------------------------------------------------------
/// @notice Calls the {FiveFiveFiveAudio} library to generate the audio data
/// and writes the WAV file to `./output/wav/rocky.wav`.
function run() public {
DynamicBufferLib.DynamicBuffer memory buffer;
// First, write the WAV file header.
buffer.p(FiveFiveFiveAudio.getAudioWavFileHeader());
// Next, we form the audio data by calling `getSoundValueAtSample` for
// each sample in the range `[0, 776*2**10)` and write each result to
// `data`.
bytes memory data = new bytes(TICKS_PER_CYCLE);
for (uint256 tick; tick < TICKS_PER_CYCLE; ) {
data[tick] = bytes1(FiveFiveFiveAudio.getSoundValueAtSample(tick));
unchecked {
++tick;
}
}
buffer.p(data);
vm.writeFile("./output/wav/rocky.wav", string(buffer.data));
}
}