投稿時間:2023-04-16 12:07:47 RSSフィード2023-04-16 12:00 分まとめ(11件)
カテゴリー等 | サイト名等 | 記事タイトル・トレンドワード等 | リンクURL | 頻出ワード・要約等/検索ボリューム | 登録日 |
---|---|---|---|---|---|
AWS | AWSタグが付けられた新着投稿 - Qiita | ALBを1 AZ, 1 subnetで使う方法 with Terraform | https://qiita.com/Kobayashi2019/items/0da45f21d0c27ec84559 | azsubnet | 2023-04-16 11:59:03 |
AWS | AWSタグが付けられた新着投稿 - Qiita | 共有されたCloud9環境をコンソールに表示する | https://qiita.com/a_b_/items/910210d863b13362a5be | cloud | 2023-04-16 11:51:04 |
技術ブログ | Developers.IO | ChatGPTの履歴データがExportできるようになっていました | https://dev.classmethod.jp/articles/chatgpt-history-data-can-now-be-exported/ | chatgpt | 2023-04-16 02:50:27 |
海外TECH | DEV Community | Visualizing Audio as a Waveform in React | https://dev.to/ssk14/visualizing-audio-as-a-waveform-in-react-o67 | Visualizing Audio as a Waveform in ReactAudio visualization is a great way to add a new dimension to music podcasts or any other audio content A common way to visualize audio is to display its waveform which shows the amplitude of the sound wave at each point in time In this tutorial we ll learn how to generate a waveform for an audio file in React without using any libraries Demo PrerequisitesBefore we get started make sure you have the following installed on your machine Node js and npm or yarn Setting up the ProjectLet s start by creating a new React project Open your terminal and run the following commands npx create react app audio visualizercd audio visualizernpm install Analyzing Audio DataTo create a waveform we need to analyze the audio data We can do this using the Web Audio API which provides an AnalyserNode object that can be used to analyze the frequency and time domain data of an audio source In our App js file we create an audioAnalyzer function that creates an AnalyserNode object and connects it to our audio source We also create a dataArray to store the frequency data that we will use to draw the waveform App jsimport useRef useState from react import styles css import WaveForm from WaveForm export default function App const audioUrl setAudioUrl useState const analyzerData setAnalyzerData useState null const audioElmRef useRef null audioAnalyzer function analyzes the audio and sets the analyzerData state const audioAnalyzer gt create a new AudioContext const audioCtx new window AudioContext window webkitAudioContext create an analyzer node with a buffer size of const analyzer audioCtx createAnalyser analyzer fftSize const bufferLength analyzer frequencyBinCount const dataArray new UintArray bufferLength const source audioCtx createMediaElementSource audioElmRef current source connect analyzer source connect audioCtx destination source onended gt source disconnect set the analyzerData state with the analyzer bufferLength and dataArray setAnalyzerData analyzer bufferLength dataArray onFileChange function handles the file input and triggers the audio analysis const onFileChange e gt const file e target files if file return setAudioUrl URL createObjectURL file audioAnalyzer return lt div className App gt lt h gt Audio Visualizer lt h gt analyzerData amp amp lt WaveForm analyzerData analyzerData gt lt div style height display flex justifyContent space around alignItems center gt lt input type file accept audio onChange onFileChange gt lt audio src audioUrl controls ref audioElmRef gt lt div gt lt div gt Drawing the WaveformNext we will create a WaveForm component that takes the analyzerData as a prop and renders a canvas element We use the useSize hook to set the size of the canvas based on the window dimensions useSize jsimport useCallback useEffect useState from react custom hook to get the width and height of the browser windowconst useSize gt initialize width and height to const width setWidth useState const height setHeight useState setSizes callback function to update width and height with current window dimensions const setSizes useCallback gt setWidth window innerWidth setHeight window innerHeight setWidth setHeight add event listener for window resize and call setSizes useEffect gt window addEventListener resize setSizes setSizes return gt window removeEventListener resize setSizes setSizes return width and height return width height export default useSize In the WaveForm component we create an animateBars function that uses the AnalyserNode to get the frequency data and draws the bars of the waveform We then create a draw function that sets up the canvas context and calls animateBars on each animation frame This function takes in the audio data analyzes it and generates a waveform that is visualized on a canvas element function animateBars analyser canvas canvasCtx dataArray bufferLength Analyze the audio data using the Web Audio API s getByteFrequencyData method analyser getByteFrequencyData dataArray Set the canvas fill style to black canvasCtx fillStyle Calculate the height of the canvas const HEIGHT canvas height Calculate the width of each bar in the waveform based on the canvas width and the buffer length var barWidth Math ceil canvas width bufferLength Initialize variables for the bar height and x position let barHeight let x Loop through each element in the dataArray for var i i lt bufferLength i Calculate the height of the current bar based on the audio data and the canvas height barHeight dataArray i HEIGHT Generate random RGB values for each bar const maximum const minimum var r Math floor Math random maximum minimum minimum var g Math floor Math random maximum minimum minimum var b Math floor Math random maximum minimum minimum Set the canvas fill style to the random RGB values canvasCtx fillStyle rgb r g b Draw the bar on the canvas at the current x position and with the calculated height and width canvasCtx fillRect x HEIGHT barHeight barWidth barHeight Update the x position for the next bar x barWidth The animateBars function is responsible for generating a waveform based on the audio data passed in It uses the Web Audio API s getByteFrequencyData method to analyze the audio data and then loops through each element in the dataArray to generate a bar for each frequency The height of each bar is calculated based on the audio data and the canvas height and the width of each bar is calculated based on the canvas width and the buffer length The function also generates random RGB values for each bar and sets the canvas fill style accordingly before drawing each bar on the canvas WaveForm jsximport useRef useEffect from react Function to animate the barsfunction animateBars analyser canvas canvasCtx dataArray bufferLength Component to render the waveformconst WaveForm analyzerData gt Ref for the canvas element const canvasRef useRef null const dataArray analyzer bufferLength analyzerData Function to draw the waveform const draw dataArray analyzer bufferLength gt const canvas canvasRef current if canvas analyzer return const canvasCtx canvas getContext d const animate gt requestAnimationFrame animate canvas width canvas width animateBars analyzer canvas canvasCtx dataArray bufferLength animate Effect to draw the waveform on mount and update useEffect gt draw dataArray analyzer bufferLength dataArray analyzer bufferLength Return the canvas element return lt canvas style position absolute top left zIndex ref canvasRef width window innerWidth height window innerHeight gt export default WaveForm ConclusionWe have explored the process of visualizing an audio waveform in a React application without the use of any external library We began by analyzing the audio using the Web Audio API and extracting the frequency data using an AnalyserNode We then used the Canvas API to draw the waveform on a canvas element Finally we implemented a function to animate the bars of the waveform by generating random colors and heights for each bar This project is a great example of the power and versatility of React and the Web Audio API To see the full code for this project please visit Codesandbox here Thank you for reading | 2023-04-16 02:11:50 |
海外TECH | CodeProject Latest Articles | C# Multithreading and Events | https://www.codeproject.com/Articles/886223/Csharp-Multithreading-and-Events | events | 2023-04-16 02:58:00 |
海外ニュース | Japan Times latest articles | Suspect’s home raided as police probe explosive used in Kishida speech attack | https://www.japantimes.co.jp/news/2023/04/16/national/kishida-explosion-investigation/ | Suspect s home raided as police probe explosive used in Kishida speech attackKishida was unharmed in the attack with his security detail taking him to safety shortly before the sound of an explosion rang out and the | 2023-04-16 11:52:55 |
ニュース | BBC News - Home | New smart motorway plans being scrapped | https://www.bbc.co.uk/news/uk-65288852?at_medium=RSS&at_campaign=KARANGA | accidents | 2023-04-16 02:21:40 |
ニュース | BBC News - Home | Public emergency message released ahead of UK test | https://www.bbc.co.uk/news/uk-65290166?at_medium=RSS&at_campaign=KARANGA | system | 2023-04-16 02:01:43 |
ビジネス | 東洋経済オンライン | いちい信用金庫に16台「おもしろ自販機」の正体 「岐阜タンメンのにんにく餃子」ほか約200点 | 食品 | 東洋経済オンライン | https://toyokeizai.net/articles/-/665469?utm_source=rss&utm_medium=http&utm_campaign=link_back | 愛知県一宮市 | 2023-04-16 11:30:00 |
ビジネス | プレジデントオンライン | こうして悲劇は繰り返される…30年前に桜田淳子さんと合同結婚式に参加した信者たちのいま【2022下半期BEST5】 - 彼女たちは「霊感商法の取り立て人」となっていた | https://president.jp/articles/-/68551 | 取り立て | 2023-04-16 12:00:00 |
ビジネス | プレジデントオンライン | 武田信玄を描いた肖像画はどれも武田信玄とはいえない…最新研究でわかった「戦国最強武将」の本当の姿 - 「ダルマのように恰幅のいい武将像」はウソだった | https://president.jp/articles/-/68418 | 戦国時代 | 2023-04-16 12:00:00 |
コメント
コメントを投稿