如果您曾经想改变歌曲的音高而不改变其速度,这篇博文适合您。变调是音乐家、DJ 和音频工程师的常见任务。在本教程中,我们将探索如何使用 Python 和 pydub 库降低歌曲的音调,并将此过程自动应用于文件夹中的多首歌曲。
为什么要进行变调?
在音乐中,变调意味着改变歌曲的音调(升高或降低)而不加快或减慢它的速度。这可用于:
将一首歌的调与另一首歌相匹配
将歌曲移调至不同调的乐器
创建混音或混搭
您需要的工具
我们将使用 Python 库 pydub 来操作音频文件。您可以使用 pip 安装它:
pip install pydub
此外,pydub 需要 ffmpeg 来处理 MP3 等音频文件。您可以通过终端安装 ffmpeg:
sudo apt install ffmpeg
变调分步指南
现在让我们深入研究自动为文件夹中的多首歌曲进行变调的 Python 脚本。该脚本循环遍历歌曲文件夹中的文件,将它们调低半音(半音 = -1),并将新文件保存到输出文件夹。
守则
import os from pydub import AudioSegment # Function to shift pitch down def pitch_shift(audio, semitones): # Adjust sample rate to shift pitch new_sample_rate = int(audio.frame_rate * (2.0 ** (semitones / 12.0))) return audio._spawn(audio.raw_data, overrides={'frame_rate': new_sample_rate}).set_frame_rate(audio.frame_rate) # Input and output folders input_folder = './songs' output_folder = './output' # Ensure the output folder exists os.makedirs(output_folder, exist_ok=True) # Loop through all files in the songs folder for filename in os.listdir(input_folder): # Check if the file is an audio file (e.g., mp3 or wav) if filename.endswith(".mp3") or filename.endswith(".wav"): # Construct the full file path input_path = os.path.join(input_folder, filename) output_path = os.path.join(output_folder, filename) # Load the audio file audio = AudioSegment.from_file(input_path) # Shift pitch down by a half-step (semitone = -1) shifted_audio = pitch_shift(audio, -1) # Export the pitch-shifted audio to the output folder shifted_audio.export(output_path, format="mp3") print(f"Processed and saved: {output_path}")
解释
导入库:
我们导入 os 来处理文件目录,并从 pydub 导入 AudioSegment 来操作音频文件。变调功能:
itch_shift 函数调整音频的采样率。当我们改变采样率时,音调就会改变。在这种情况下,我们使用以下公式计算新的采样率,将音高降低一个半音:
new_sample_rate = int(audio.frame_rate * (2.0 ** (半音 / 12.0)))
输入和输出文件夹:
我们定义将读取音频文件并保存变调版本的文件夹。如果输出文件夹不存在,则会创建它。-
循环播放歌曲:
使用 os.listdir(),我们循环遍历歌曲文件夹中的每个文件。该脚本在处理文件之前会检查该文件是否是音频文件(.mp3 或 .wav)。对于每个文件:- 它加载音频。
- 应用pitch_shift功能,将音高降低半步。
- 音高变换后的音频将导出到输出文件夹。
导出和反馈:
处理完成后,变调后的歌曲将保存在输出文件夹中,并打印一条确认消息。
运行脚本
确保歌曲文件夹中有音频文件,然后运行脚本:
python -m pitch_down.py
移调后的文件将保存在输出文件夹中。
定制化
您可以轻松地将此脚本修改为:
通过传递正值来提高音频的音调(例如,pitch_shift(audio, 1) 表示升高半步)。
通过在条件检查中添加其他扩展名(如 .ogg 或 .flac)来处理不同的文件格式。
通过调整半音参数来移动不同数量的半音。
结论
该脚本是一种使用 Python 对多个音频文件进行音高转换的简单而强大的方法。使用 pydub 和 ffmpeg,您可以批量操作音频文件,从而使音乐家、制作人或任何音频处理人员更轻松地完成音调校正或音频准备等任务。
请随意尝试此脚本,看看如何使其适应您的需求。快乐编码!
以上是如何使用 Python 降低歌曲的音调的详细内容。更多信息请关注PHP中文网其他相关文章!

pythonisehybridmodelofcompilationand interpretation:1)thepythoninterspretercompilesourcececodeintoplatform- interpententbybytecode.2)thepytythonvirtualmachine(pvm)thenexecuteCutestestestesteSteSteSteSteSteSthisByTecode,BelancingEaseofuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允许fordingfordforderynamictynamictymictymictymictyandrapiddefupment,尽管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知识之际,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations则youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

concateNateListsinpythonwithTheSamelements,使用:1)operatototakeepduplicates,2)asettoremavelemavphicates,or3)listCompreanspearensionforcontroloverduplicates,每个methodhasdhasdifferentperferentperferentperforentperforentperforentperfortenceandordormplications。

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允许ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

禅工作室 13.0.1
功能强大的PHP集成开发环境

WebStorm Mac版
好用的JavaScript开发工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Dreamweaver CS6
视觉化网页开发工具