Skip to Content
《Arduino 烹饪书》第三版
book

《Arduino 烹饪书》第三版

by Michael Margolis, Brian Jepson, Nicholas Robert Weldin
May 2025
Beginner to intermediate
798 pages
10h 10m
Chinese
O'Reilly Media, Inc.
Content preview from 《Arduino 烹饪书》第三版

音频输出

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

9.0 引言

谷歌上关于 "Arduino 音乐项目 "的搜索结果数以百万计,而本章仅有篇幅介绍 Arduino 音频技术和一些示例,希望能帮助大家入门。Arduino 并不是为了成为一个复杂的音频合成器而制造的,但它肯定可以通过扬声器等输出设备产生声音。

本章将介绍如何制作噪音、播放预先录制的声音、制作一些简单的输出,甚至尝试声音合成。

如果您想为自己的项目寻找灵感,请访问音乐项目网站:

以下是两个极好的例子,说明了使用 Arduino、一些硬件和大量创意可以实现什么:

声音是由空气振动产生的。如果振动有规律地重复,声音就会有独特的音高。Arduino 可以通过驱动扬声器 或压电器件 (一种小型陶瓷换能器,脉冲时会产生声音)来产生声音,将电子振动转换为扬声器脉冲,从而振动空气。声音的音调(频率)由扬声器脉冲输入和输出所需的时间决定;时间越短,频率越高。

提示

您可能会遇到两种类型的压电扬声器。压电扬声器可以产生不同频率的声音。压电蜂鸣器( )则包含振荡器电路,通电时会以固定频率发出蜂鸣。本章中基于压电技术的解决方案假定您使用的是压电扬声器,而不是蜂鸣器。

频率的单位是赫兹,它指的是信号在一秒钟内重复周期的次数。人类的听力范围从 20 赫兹(Hz)左右到 20,000 赫兹(但因人而异,并随年龄而变化)。

Arduino 软件包含一个用于发出声音的tone 函数。配方9.19.2演示了如何使用该函数发出声音和曲调。tone 函数使用硬件定时器。在标准 Arduino 板(Uno 和类似板)上,一次只能发出一个音调。当您使用tone 函数时,它将占用analogWrite 在引脚 3 和 11 上使用的定时器,因此如果您需要模拟输出,则需要选择不同的引脚。为了克服这一限制,Recipe 9.3演示了如何使用增强音色库来产生多个音色,Recipe 9.4演示了如何在不使用tone 函数或硬件定时器的情况下产生声音。

扬声器脉冲所能产生的声音有限,听起来也没有什么音乐感。输出是方波 (见图 9-1),听起来很刺耳,更像是古董电脑游戏,而不是乐器。

如果没有外部硬件,Arduino Uno 等基本电路板很难产生音乐性更复杂的声音。您可以添加一个可扩展 Uno 功能的屏蔽 ,如 Adafruit Wave Shield,以播放屏蔽上存储卡中的音频文件。

一些较新的 Arduino 板具有数模转换器输出 (DAC),可产生高质量音频--既可播放 SD 卡中的声音文件,也可在软件中合成声音(见配方 1.8)。较新电路板的另一个选项是I2S(Inter-IC Sound)。这是一种用于与外部芯片通信的数字接口,可产生高质量的立体声音频接口(包括输入和输出)。

您还可以使用 Arduino 控制为发出声音而构建的外部设备。配方 9.5展示了如何向 MIDI 设备发送乐器数字接口 (MIDI) 信息。这些设备能发出各种乐器的高品质声音,并能同时发出多种乐器的声音。配方 9.5中的草图展示了如何生成 MIDI 信息以播放音阶。

利用数字脉冲产生声音

配方 9.6概述了一款名为 Auduino 的应用程序,它使用复杂的软件处理来合成声音。配方 9.7 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Make a Raspberry Pi-Controlled Robot

Make a Raspberry Pi-Controlled Robot

Wolfram Donat

Publisher Resources

ISBN: 9798341659582